版本更新实现的思路
- 获取自身的版本号
- 获取AppStore的版本号
- 自身的版本号和AppStore的比较
- 弹窗提示所需数据的获取的方式
- 1.获取自身的版本号
2.AppStore的版本号
WechatIMG58.jpeg具体实现的代码
-
网络请求app的信息
-(void)VersonUpdate{
//定义的app的地址
NSString *urld = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"你的APPID"]; //网络请求app的信息,主要是取得我说需要的Version
NSURL *url = [NSURL URLWithString:urld];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setHTTPMethod:@"POST"]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
if (data) { //data是有关于App所有的信息
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) { [receiveStatusDic setValue:@"1" forKey:@"status"];
[receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"]; //请求的有数据,进行版本比较
[self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
}else{ [receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}]; [task resume];
} - 获取自身的版本号并与AppStore比较
-(void)receiveData:(id)sender
{
//获取APP自身版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"]; NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."]; if ((versionArray.count == 3) && (localArray.count == versionArray.count)) { if ([localArray[0] intValue] < [versionArray[0] intValue]) {
[self updateVersion];
}else if ([localArray[0] intValue] == [versionArray[0] intValue]){
if ([localArray[1] intValue] < [versionArray[1] intValue]) {
[self updateVersion];
}else if ([localArray[1] intValue] == [versionArray[1] intValue]){
if ([localArray[2] intValue] < [versionArray[2] intValue]) {
[self updateVersion];
}
}
}
}
}- 根据比较的结果,实现弹窗
-(void)updateVersion{
NSString *msg = [NSString stringWithFormat:@"更新最新版本,优惠信息提前知"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"你的app在商店的下载地址"]];
[[UIApplication sharedApplication]openURL:url];
}];
[alertController addAction:otherAction];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil]; }温馨提示
- 我这种实现的方式是根据自身的版本和线上版本,比较大小来实现的弹窗,如果比线上的版本小,就会弹窗,具体的比较看上面的代码。这样上线可以瞒天过海。
- 当你想实现提醒更新的时候,直接的把我上面的代码,copy到AppDelegate里面即可,当然还要换上你自己的appid,具体的获取方式,上面已经写明。
- 如果想模拟器测试效果,只要把version改的比线上的版本小就可以。
- 我的弹窗比较“简陋”,具体的好看的效果,还需要自己去改变了。
-