关于检测更新,苹果审核是不允许弹出提示更新的提示的,所以可以后台设置应用商店里面已经上架的appid,等上架后再改回新版的appid。
关于自动提醒更新,直接上代码:
#import"ViewController.h"//1一定要先配置自己项目在商店的APPID,配置完最好在真机上运行才能看到完全效果哦
#defineSTOREAPPID @"1080182980"
@interfaceViewController ()@end@implementationViewController- (void)viewDidLoad {
[super viewDidLoad];//一句代码实现检测更新[self hsUpdateApp];
}/**
* 天朝专用检测app更新*/-(void)hsUpdateApp
{//2先获取当前工程项目版本号NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
NSString*currentVersion=infoDic[@"CFBundleShortVersionString"];//3从网络获取appStore版本号NSError *error;
NSData*response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]] returningResponse:nil error:nil];if(response ==nil) {
NSLog(@"你没有连接网络哦");return;
}
NSDictionary*appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];if(error) {
NSLog(@"hsUpdateAppError:%@",error);return;
}
NSArray*array = appInfoDic[@"results"];
NSDictionary*dic = array[0];
NSString*appStoreVersion = dic[@"version"];//打印版本号NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);//4当前版本号小于商店版本号,就更新if([currentVersion floatValue] <[appStoreVersion floatValue])
{
UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"版本有更新"message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",appStoreVersion]delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];
[alert show];
}else{
NSLog(@"版本号好像比商店大噢!检测到不需要更新");
}
}
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{//5实现跳转到应用商店进行更新if(buttonIndex==1)
{//6此处加入应用在app store的地址,方便用户去更新,一种实现方式如下:NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", STOREAPPID]];
[[UIApplication sharedApplication] openURL:url];
}
}@end