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

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

原来项目中提示版本更新是用友盟做的,后来友盟(苹果)的后台不支持了,所以需要自己来做。当然前期我们自己做的版本更新,后来想了想,因为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就是没有新版本

 

}

转载于:https://my.oschina.net/u/3537744/blog/1623341

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值