获取app版本URL 数字是appID,在开发者账号app信息中可以找到
#define APP_URL @"http://itunes.apple.com/cn/lookup?id=1169362912"
检测商店的版本
//检测商店的版本 AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager]; [mgr POST:APP_URL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSLog(@"------------------------------------------------------------------------%@",responseObject); /*responseObject是个字典{},有两个key KEYresultCount = 1//表示搜到一个符合你要求的APP results =()//这是个只有一个元素的数组,里面都是app信息,那一个元素就是一个字典。里面有各种key。其中有 trackName (名称)trackViewUrl = (下载地址)version (可显示的版本号)等等 */ //具体实现为 NSArray *arr = [responseObject objectForKey:@"results"]; NSDictionary *dic = [arr firstObject]; NSString *versionStr = [dic objectForKey:@"version"]; NSString *trackViewUrl = [dic objectForKey:@"trackViewUrl"]; NSString *releaseNotes = [dic objectForKey:@"releaseNotes"];//更新日志 //NSString* buile = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString*) kCFBundleVersionKey];build号 // 加测当前的版本 NSString *app_Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; NSLog(@"------------------------------------------------------------------------%@",app_Version); if ([self compareVersionsFormAppStore:versionStr WithAppVersion:app_Version]) { UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"发现新版本:%@",versionStr] message:releaseNotes preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了取消"); }]; UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"点击了知道了"); NSURL * url = [NSURL URLWithString:trackViewUrl];//itunesURL = trackViewUrl的内容 [[UIApplication sharedApplication] openURL:url]; }]; [alertVC addAction:cancelAction]; [alertVC addAction:OKAction]; [self presentViewController:alertVC animated:YES completion:nil]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@""); }];
比较方法
//比较版本的方法,在这里我用的是Version来比较的 - (BOOL)compareVersionsFormAppStore:(NSString*)AppStoreVersion WithAppVersion:(NSString*)AppVersion { BOOL littleSunResult = false; NSMutableArray* a = (NSMutableArray*) [AppStoreVersion componentsSeparatedByString: @"."]; NSMutableArray* b = (NSMutableArray*) [AppVersion componentsSeparatedByString: @"."]; while (a.count < b.count) { [a addObject: @"0"]; } while (b.count < a.count) { [b addObject: @"0"]; } for (int j = 0; j<a.count; j++) { if ([[a objectAtIndex:j] integerValue] > [[b objectAtIndex:j] integerValue]) { littleSunResult = true; break; }else if([[a objectAtIndex:j] integerValue] < [[b objectAtIndex:j] integerValue]){ littleSunResult = false; break; }else{ littleSunResult = false; } } return littleSunResult;//true就是有新版本,false就是没有新版本 }