您的位置:首页 > 移动开发

iOS APP如何实现版本检测更新

2016-06-16 12:04 549 查看
原来项目中提示版本更新是用友盟做的,后来友盟(苹果)的后台不支持了,所以需要自己来做。当然前期我们自己做的版本更新,后来想了想,因为iOS取到只有一个( 企业版的也不多)。所以我就百度了一下,然后用苹果给咱们的rest接口做了检测更新。

原理:拿到苹果服务器我们产品信息(包含版本号version和buile 还有产品的所有信息)跟当前安装的产品的版本进行比较。

步骤:

1.取到苹果服务器我们产品的信息的地址。

注意:下面的这个APP_URL地址中有个/cn 因为我的只在国内销售。如果像是“微信”等应用的话就是@”http://itunes.apple.com/lookup?id=微信的appid”(aped怎么找啊,有好多办法,可以在你填写信息的后台自己看 apple ID随机生成码就是)

2、根据上面的地址请求需要的信息

//#define APP_URL @"http://itunes.apple.com/cn/lookup?id=1093039842"

//检测更新
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* thisVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

if ([self compareVersionsFormAppStore:versionStr AppVersion:thisVersion]) {
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就是没有新版本

}


3、总结(注意点):1>我用的是AFN 3.1.0版本(兼容IP6)所以用的是AFHTTPSessionManager对象; 2>UIAlertView弃用了,说实话我工程里用的都是它,因为当时没弃用。现在用的就是这个。见怪不怪,就是更好用了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息