iOS版本检测更新2018

iOS版本检测更新

// 先获取当前工程项目版本号
    NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
    NSLog(@"%@",infoDic);
    NSString *currentVersion = infoDic[@"CFBundleShortVersionString"];
    
    
    // 从网络获取AppStore版本号
    __block NSError *jsonError;
    // STOREAPPID是你在AppStore对应自己App的ID 可在AppStore上找到 也可在iTunesConnect上找到
    NSURLSession *session = [NSURLSession sharedSession];
    [session dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@",STORE_APPID]]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&jsonError];
        if (jsonError) {
            NSLog(@"UpdateAppError:%@",jsonError);
            return;
        }
        
        NSArray *array = appInfoDic[@"results"];
        if (array.count < 1) {
            NSLog(@"此APPID为未上架的APP或者查询不到");
            return;
        }
        
        NSDictionary *dic = array[0];
        NSString *appStoreVersion = dic[@"version"];
        //打印版本号
        NSLog(@"当前版本号:%@\n商店版本号:%@",currentVersion,appStoreVersion);
        
        
        // 当前版本号小于商店版本号,就更新
        if([currentVersion floatValue] < [appStoreVersion floatValue]) {
            UIAlertController *alercConteoller = [UIAlertController alertControllerWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"检测到新版本(%@),是否更新?",dic[@"version"]] preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *actionYes = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //此处加入自己应用在AppStore的地址,方便用户去更新,一种实现方式如下
                NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/简书-创作你的创作/id888237539?mt=8"];
                
                [[UIApplication sharedApplication] openURL:url];
            }];
            UIAlertAction *actionNo = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alercConteoller addAction:actionYes];
            [alercConteoller addAction:actionNo];
            [self presentViewController:alercConteoller animated:YES completion:nil];
        }else{
            NSLog(@"版本号比商店大 检测到不需要更新");
        }
        
    }];

猜你喜欢

转载自blog.csdn.net/wenzfcsdn/article/details/81137136