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"];
        }
        // 获取当前的版本
        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(@"当前版本====%f Appstroe版本====%f",bundleVersion,appVersion);
            
            if (appVersion > bundleVersion) {
                
                UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"有新的版本更新,是否前往更新?" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    [alertVc dismissViewControllerAnimated:YES completion:nil];
                }];
                [alertVc addAction:cancelAction];
                UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"前往" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    //跳转到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];
    
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在苹果的 app store 条文中,是不允许新版本提示的, 但是只要不让那些审核员知道有这个功能就可以了。启动的...
    圣斗士皮皮阅读 4,432评论 0 5
  • 基于良好的用户体验,当在appStroe上线了新的版本时,提醒用户更新”新版本“是必不可少的。 版本更新的...
    ninazhang阅读 4,664评论 5 1
  • 之前appstore会自动提示更新 最近两次不知道为什么不提示了 那没办法 后台也没提供对比接口 自己去判断一下当...
    光头金项链的和尚阅读 4,075评论 0 0
  • 窗外的树枝摇曳着,树叶飘零,不曾想过冬天那么快就到了。走在冬季的冷风中,风从指缝间穿过,扑打在脸上,冷中夹杂着疼痛...
    木木子花阅读 1,248评论 0 1
  • 1. 我坐在出租车上哭的稀里哗啦,师傅从后视镜偷偷的瞄了我好几眼,欲言又止,估计他是没见过哭的这么伤心的姑娘,鼻涕...
    蓝橙LC阅读 5,451评论 44 35