2019-03-03

    NSString*appid =@"1450105728";

    NSString * url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",appid];//替换为自己App的ID

    // 获取本地版本号

    NSString * currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];

    // 网络请求获取最新版本

    [JWHttpRequest getWithStrPath:url withParameters:mdic withSuccess:^(id data) {

        NSArray* results = data[@"results"];

        if(results && results.count>0)

        {

            NSDictionary* dic = results.firstObject;

            NSString* lineVersion = dic[@"version"];//版本号

            NSString* releaseNotes = dic[@"releaseNotes"];//更新说明

            //NSString * trackViewUrl = dic[@"trackViewUrl"];//链接

            //把版本号转换成数值

            NSArray* array1 = [currentVersioncomponentsSeparatedByString:@"."];

            NSIntegercurrentVersionInt =0;

            if(array1.count==3)//默认版本号1.0.0类型

            {

                currentVersionInt = [array1[0]integerValue]*100+ [array1[1]integerValue]*10+ [array1[2]integerValue];

            }

            NSArray* array2 = [lineVersioncomponentsSeparatedByString:@"."];

            NSIntegerlineVersionInt =0;

            if(array2.count==3)

            {

                lineVersionInt = [array2[0]integerValue]*100+ [array2[1]integerValue]*10+ [array2[2]integerValue];

            }

            if(lineVersionInt > currentVersionInt)//线上版本大于本地版本

            {

                UIAlertController * alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"发现新版本%@",lineVersion] message:releaseNotes preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction * ok = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];

                UIAlertAction * update = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

                    //跳转到App Store

                    NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",appid];

                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];

                }];

                [alertaddAction:ok];

                [alertaddAction:update];

                [selfpresentViewController:alert animated:YES completion:nil];

            }

        }

    }withFialure:^(NSError*error,NSString*status,NSString*message) {

        NSLog(@"版本更新出错,%@",error.description);

    }];

你可能感兴趣的:(2019-03-03)