iOS
想要检查 App
当前版本是否为最新,一般的方案大概都是服务器自己提供一个接口来获取 App
最新版本是多少,然后再做出相应提示是否需要更新,但是接口需要手动维护,应用要审核,还得等审核通过以后才能更新版本号,其实苹果提供了一个 iTunes
接口,能够查到 App
在 AppStore
上的状态信息,既省事又准确,下面记录一下具体实现方法。
接口信息
- 这是 iTunes 接口地址 ,有兴趣可以看一下,我们要用到的接口如下,
xxx
处换成自己 App
的 AppId
,AppId
可以在 iTunes Connect
里面看到。
http://itunes.apple.com/lookup?id=xxx
|
{ "resultCount" : 1, "results" : [{ "artistId" : "开发者 ID", "artistName" : "开发者名称", "trackCensoredName" : "审查名称", "trackContentRating" : "评级", "trackId" : "应用程序 ID", "trackName" = "应用程序名称", "trackViewUrl" = "应用程序下载网址", "userRatingCount" = "用户评论数量", "userRatingCountForCurrentVersion" = "当前版本的用户评论数量", "version" = "版本号" }] }
|
实现方法
下面是检查版本更新的具体实现方法,注意接口地址 xxx
处换成自己 App
的 AppId
,App
审核的时候版本肯定是比 AppStore
上高的,所以不用担心审核时会跳出更新提示。
- (void)checkVersion { NSString *url = @"http://itunes.apple.com/lookup?id=xxx"; [[AFHTTPSessionManager manager] POST:url parameters:nil progress:nil success:^(NSURLSessionDataTask *task, id responseObject) { DLog(@"版本更新检查成功"); NSArray *results = responseObject[@"results"]; if (results && results.count > 0) { NSDictionary *response = results.firstObject; NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; NSString *lastestVersion = response[@"version"]; if (currentVersion && lastestVersion && ![self isLastestVersion:currentVersion compare:lastestVersion]) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"检测到有版本更新,是否前往 AppStore 更新版本。" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { NSString *trackViewUrl = response[@"trackViewUrl"]; if (trackViewUrl) { NSURL *appStoreURL = [NSURL URLWithString:trackViewUrl]; if ([[UIApplication sharedApplication] canOpenURL:appStoreURL]) { [[UIApplication sharedApplication] openURL:appStoreURL]; } } }]]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [self.window.rootViewController presentViewController:alert animated:YES completion:nil]; } } } failure:^(NSURLSessionDataTask *task, NSError *error) { DLog(@"版本更新检查失败"); }]; }
|
- (BOOL)isLastestVersion:(NSString *)currentVersion compare:(NSString *)lastestVersion { if (currentVersion && lastestVersion) { NSMutableArray *currentItems = [[currentVersion componentsSeparatedByString:@"."] mutableCopy]; NSMutableArray *lastestItems = [[lastestVersion componentsSeparatedByString:@"."] mutableCopy]; NSInteger currentCount = currentItems.count; NSInteger lastestCount = lastestItems.count; if (currentCount != lastestCount) { NSInteger count = labs(currentCount - lastestCount); for (int i = 0; i < count; ++i) { if (currentCount > lastestCount) { [lastestItems addObject:@"0"]; } else { [currentItems addObject:@"0"]; } } } BOOL isLastest = YES; for (int i = 0; i < currentItems.count; ++i) { NSString *currentItem = currentItems[i]; NSString *lastestItem = lastestItems[i]; if (currentItem.integerValue != lastestItem.integerValue) { isLastest = currentItem.integerValue > lastestItem.integerValue; break; } } return isLastest; } return NO; }
|