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

#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;

            }elseif([[a objectAtIndex:j] integerValue] < [[b objectAtIndex:j] integerValue]){

                littleSunResult = false;

                break;

            }else{

                littleSunResult = false;

            }

        }

        return littleSunResult;//true就是有新版本,false就是没有新版本

        

    }




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值