iOS 檢測版本更新

/**
 檢測版本更新

 @param appId AppID
 */
- (void)testVersionUpdate:(NSString *)appId{
    
    NSString *url = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@",appId];//這里的ID是APPID
    NSString *urlStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.securityPolicy = [AFSecurityPolicy defaultPolicy];
    manager.securityPolicy.allowInvalidCertificates = YES;//忽略https證書
    manager.securityPolicy.validatesDomainName = NO;//是否驗證域名
    manager.responseSerializer = [AFHTTPResponseSerializer  serializer];
    
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DLog(@"App=====%@",dict);
        NSMutableArray *array = dict[@"results"];
        NSString *str;
        for (NSDictionary *dic in array) {
            
            str = dic[@"version"];
        }
        // 獲取當(dāng)前的版本
        NSDictionary *infoDict = [[NSBundle mainBundle]infoDictionary];
        NSString *currentVersion = [infoDict objectForKey:@"CFBundleShortVersionString"];
        
        //        NSLog(@"======%@",infoDict);
        NSArray * bundleArray = [currentVersion componentsSeparatedByString:@"."];
        
        NSString *bundleStr = @"";
        
        for (int i = 0; i<bundleArray.count; i++) {
            
            if (i == 0) {
                
                bundleStr = [NSString stringWithFormat:@"%@.",bundleArray[0]];
                
            }else{
                
                bundleStr = [NSString stringWithFormat:@"%@%@",bundleStr,bundleArray[i]];
            }
        }
        CGFloat bundleVersion = [bundleStr floatValue];
        if ([str isEqualToString:@"(null)"]||str.length == 0 || [str isEqualToString:@"null"]  ) {
            
            NSLog(@"====%f",bundleVersion);
            
        }else{

            NSArray * appArray = [str componentsSeparatedByString:@"."];
            NSString *appStr = @"";
            for (int i = 0; i<appArray.count; i++) {
                
                if (i == 0) {
                    
                    appStr = [NSString stringWithFormat:@"%@.",appArray[0]];
                }else{
                    
                    appStr = [NSString stringWithFormat:@"%@%@",appStr,appArray[i]];
                }
            }
            CGFloat appVersion = [appStr floatValue];
            
            DLog(@"當(dāng)前版本====%f Appstroe版本====%f",bundleVersion,appVersion);
            
            if (appVersion > bundleVersion) {
                
                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"溫馨提示" message:@"有新的版本更新,是否前往更新?" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"關(guān)閉" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    [alertVc dismissViewControllerAnimated:YES completion:nil];
                }];
                [alertVc addAction:cancelAction];
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    //跳轉(zhuǎn)到AppStore,該App下載界面
                    NSString * trackViewUrl = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/id%@?mt=8", appId];
                    NSURL *url = [NSURL URLWithString:trackViewUrl];
                    [[UIApplication sharedApplication] openURL:url];
                    
                }];
                [alertVc addAction:okAction];
                [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alertVc animated:YES completion:nil];
                
            }
            
        }
    } failure:nil];
    
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在蘋果的 app store 條文中,是不允許新版本提示的, 但是只要不讓那些審核員知道有這個功能就可以了。啟動的...
    圣斗士皮皮閱讀 972評論 0 5
  • 基于良好的用戶體驗,當(dāng)在appStroe上線了新的版本時,提醒用戶更新”新版本“是必不可少的。 版本更新的...
    ninazhang閱讀 1,153評論 5 1
  • 之前appstore會自動提示更新 最近兩次不知道為什么不提示了 那沒辦法 后臺也沒提供對比接口 自己去判斷一下當(dāng)...
    光頭金項鏈的和尚閱讀 806評論 0 0
  • 窗外的樹枝搖曳著,樹葉飄零,不曾想過冬天那么快就到了。走在冬季的冷風(fēng)中,風(fēng)從指縫間穿過,撲打在臉上,冷中夾雜著疼痛...
    木木子花閱讀 209評論 0 1
  • 1. 我坐在出租車上哭的稀里嘩啦,師傅從后視鏡偷偷的瞄了我好幾眼,欲言又止,估計他是沒見過哭的這么傷心的姑娘,鼻涕...
    藍橙LC閱讀 1,657評論 44 35

友情鏈接更多精彩內(nèi)容