#import <Foundation/Foundation.h>
@interface NSString (StringToWords)
- (NSArray *)words;
@end
#import "NSString+StringToWords.h"
@implementation NSString (StringToWords)
- (NSArray *)words
{
#if ! __has_feature(objc_arc)
NSMutableArray *words = [[[NSMutableArray alloc] init] autorelease];
#else
NSMutableArray *words = [[NSMutableArray alloc] init];
#endif
const char *str = [self cStringUsingEncoding:NSUTF8StringEncoding];
char *word;
for (int i = 0; i < strlen(str);) {
int len = 0;
if (str[i] >= 0xFFFFFFFC) {
len = 6;
} else if (str[i] >= 0xFFFFFFF8) {
len = 5;
} else if (str[i] >= 0xFFFFFFF0 ) {
len = 4;
} else if (str[i] >= 0xFFFFFFE0) {
len = 3;
} else if (str[i] >= 0xFFFFFFC0) {
len = 2;
} else if (str[i] >= 0x00) {
len = 1;
}
word = malloc(sizeof(char) * (len + 1));
for (int j = 0; j < len; j++) {
word[j] = str[j + i];
}
word[len] = '\0';
i = i + len;
NSString *oneWord = [NSString stringWithCString:word encoding:NSUTF8StringEncoding];
free(word);
[words addObject:oneWord];
}
return words;
}
@end
检查更新工具类
#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
@interface AppVersionUpdateManager : NSObject<UIAlertViewDelegate,ASIHTTPRequestDelegate>{
BOOL showWarn;
}
@property (nonatomic,strong) MBProgressHUD *mainHud;
//初始化
+ (id)sharedManager;
/**
* 描述:检查程序版本更新
* 参数:loadingStr:检查过程的提示
* 返回值:
*/
- (void)checkVersionUpdate:(NSString *)loadingStr;
@end
.m
//
// AppVersionUpdateManager.m
// WithBusiness
// 软件更新管理类
// Created by maple on 14-2-21.
// Copyright (c) 2014年 sinosoft. All rights reserved.
//
#import "AppVersionUpdateManager.h"
#import "AppApiConstants.h"
#import "NSString+StringToWords.h"
static AppVersionUpdateManager *defaultManager;
@implementation AppVersionUpdateManager
#pragma mark -
#pragma mark Singleton Method
+ (id)sharedManager{
@synchronized([AppVersionUpdateManager class]) {
if (!defaultManager) {
defaultManager = [[self alloc] init];
}
return defaultManager;
}
return nil;
}
- (void)checkVersionUpdate:(NSString *)loadingStr{
if (!stringIsEmpty(loadingStr)) {
showWarn = YES;
if (!self.mainHud) {
self.mainHud = [MBProgressHUD showHUDAddedTo:[AppDelegate sharedInstance].window animated:YES];
self.mainHud.mode = MBProgressHUDModeIndeterminate;
}
[self.mainHud show:YES];
}else{
showWarn = NO;
}
[self checkVersionFromeServer];
// [self checkVersionFromeAppStroe];
}
//从AppStroe更新程序
- (void)checkVersionFromeAppStroe
{
//ITUNES_CHECK_UPDATE http://itunes.apple.com/lookup?id=%@
NSString *requestString = [NSString stringWithFormat:ITUNES_CHECK_UPDATE,APP_ID];
ASIHTTPRequest *versionRequest = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:requestString]];
versionRequest = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:requestString]];
[versionRequest setRequestMethod:@"GET"];
[versionRequest setTimeOutSeconds:8.0f];
versionRequest.delegate = self;
[versionRequest startAsynchronous];
__weak ASIHTTPRequest *request = versionRequest;
__weak AppVersionUpdateManager *versionManager = self;
[versionRequest setCompletionBlock:^{
[self.mainHud hide:YES];
NSString *responseString = [request responseString];
NSDictionary *responseDit = [responseString JSONValue];
NSArray *infoArray = [responseDit objectForKey:@"results"];
if ([infoArray count]){
NSDictionary *updateInfo = [infoArray objectAtIndex:0];
NSString *newVersion = [NSString stringWithFormat:@"%@",[updateInfo objectForKey:@"version"]];
if ([versionManager comparenewVersion:newVersion]) {
NSString *msg = [NSString stringWithFormat:
ITUNE_STORE_UPDATE_INFO,
newVersion];
//releaseNotes 更新内容
msg = [NSString stringWithFormat:@"%@\n%@",msg,[updateInfo objectForKey:@"releaseNotes"]];
UIAlertView *versionAlertView = [[UIAlertView alloc] initWithTitle:WAEM_WARING_TITLE message:msg delegate:nil cancelButtonTitle:@"不更新" otherButtonTitles:@"更新新版本", nil];
[versionAlertView showWithCompletionHandler:^(NSInteger buttonIndex) {
if (buttonIndex == 1) { //更新
//打开程序在appStroe的页面
NSString *updateUrl = [updateInfo objectForKey:@"trackViewUrl"];
// NSLog(@"updateUrl = %@",updateUrl);
NSURL *url = [NSURL URLWithString:updateUrl];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
}];
}else{
if (showWarn) {
UIAlertView *versionAlertView = [[UIAlertView alloc] initWithTitle:WAEM_WARING_TITLE message:@"当前已经是最新版本" delegate:nil cancelButtonTitle:STR_CONFIRM otherButtonTitles:nil, nil];
[versionAlertView show];
}
}
}
}];
[versionRequest setFailedBlock:^{
[self.mainHud hide:YES];
if (showWarn) {
NSString *erroMsg = [[request error].userInfo objectForKey:@"NSLocalizedDescription"];
if ([erroMsg isEqualToString:@"The request timed out"]) {
[MBProgressHUD showError:@"Time out" toView:[AppDelegate sharedInstance].window];
}else{
[MBProgressHUD showError:@"加载错误" toView:[AppDelegate sharedInstance].window];
}
}
}];
}
//从服务器更新程序
- (void)checkVersionFromeServer
{
NSString *urlStr = [NSString stringWithFormat:@"%@%@", kSDLB_SYS_SERVER, Check_APPUpdateInfo];
ASIFormDataRequest *postRequest = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:urlStr]];
[postRequest setRequestMethod:@"POST"];
[postRequest setTimeOutSeconds:8.0f];
NSString *oldVersion = [NSString stringWithFormat:@"%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
NSLog(@"odlVersion = %@",oldVersion);
[postRequest setPostValue:oldVersion forKey:@"versionCode"];
[postRequest setPostValue:@"iphone" forKey:@"appPlatform"];
[postRequest startAsynchronous];
__weak ASIFormDataRequest *request = postRequest;
__weak AppVersionUpdateManager *versionManager = self;
[postRequest setCompletionBlock:^{
[self.mainHud hide:YES];
NSDictionary *resDit = [request.responseString JSONValue];
NSLog(@"updateResDit = %@", resDit);
NSString *statusCode = [resDit objectForKey:@"statusCode"];
//如果返回错误
if (getIntFromString(statusCode) != STATUS_SUCCESS) {
if (showWarn) {
[MBProgressHUD showError:@"加载错误" toView:[AppDelegate sharedInstance].window];
return ;
}
}
//返回正确
if (getIntFromString(statusCode) == STATUS_SUCCESS) {
NSDictionary *dataDit = [resDit objectForKey:@"data"];
NSString *newVersion = [dataDit objectForKey:@"versionCode"];
if ([versionManager comparenewVersion:newVersion]) {
NSString *msg = [NSString stringWithFormat:
ITUNE_STORE_UPDATE_INFO,
newVersion];
NSString *updateContent = [dataDit objectForKey:@"updateContent"];
msg = [NSString stringWithFormat:@"%@\n%@",msg, updateContent];
UIAlertView *versionAlertView = [[UIAlertView alloc] initWithTitle:WAEM_WARING_TITLE message:msg delegate:nil cancelButtonTitle:@"不更新" otherButtonTitles:@"更新新版本", nil];
[versionAlertView showWithCompletionHandler:^(NSInteger buttonIndex) {
if (buttonIndex == 1) { //更新
// NSString *updateUrl = [versionManager.updateInfo objectForKey:@"trackViewUrl"];
NSString *updateUrl = [dataDit objectForKey:@"downloadUrl"];
NSURL *url = [NSURL URLWithString:updateUrl];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
}
}
}];
}
else{
if (showWarn) {
UIAlertView *versionAlertView = [[UIAlertView alloc] initWithTitle:WAEM_WARING_TITLE message:@"当前已经是最新版本" delegate:nil cancelButtonTitle:STR_CONFIRM otherButtonTitles:nil, nil];
[versionAlertView show];
}
}
}
}];
[postRequest setFailedBlock:^{
[self.mainHud hide:YES];
if (showWarn) {
NSString *erroMsg = [[request error].userInfo objectForKey:@"NSLocalizedDescription"];
if ([erroMsg isEqualToString:@"The request timed out"]) {
[MBProgressHUD showError:@"Time out" toView:[AppDelegate sharedInstance].window];
}else{
[MBProgressHUD showError:@"加载错误" toView:[AppDelegate sharedInstance].window];
}
}
}];
}
//比较
- (BOOL)comparenewVersion:(NSString *)newVersion{
BOOL isUpdate = NO;
NSString *oldVersion = [NSString stringWithFormat:@"%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
oldVersion = [oldVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
newVersion = [newVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
NSArray * oldList= [oldVersion words];
NSArray * newList= [newVersion words];
NSMutableArray *mOldList = [[NSMutableArray alloc] initWithArray:oldList];
NSMutableArray *mNewList = [[NSMutableArray alloc] initWithArray:newList];
int oldIndex = [mOldList count];
int newIndex = [mNewList count];
if (oldIndex>newIndex) {
int compare = oldIndex - newIndex;
for (int i = 0; i<compare; i++) {
[mNewList addObject:[NSString stringWithFormat:@"%d",0]];
}
}else if (oldIndex<newIndex){
int compare = newIndex - oldIndex;
for (int i = 0; i<compare; i++) {
[mOldList addObject:[NSString stringWithFormat:@"%d",0]];
}
}
for (int i = 0; i<[mOldList count]; i++) {
int oldValue = [[mOldList objectAtIndex:i] intValue];
int newValue = [[mNewList objectAtIndex:i] intValue];
if (oldValue < newValue) {
isUpdate = YES;
break;
}else if (oldValue > newValue){
isUpdate = NO;
break;
}
}
return isUpdate;
}
@end