iOS App内检测版本更新

    NSString *version = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    [mgr.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"id"] = @"123456789";// 你程序的apple ID号
    [mgr POST:@"http://itunes.apple.com/cn/lookup?id=123456789" parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        // App_URL http://itunes.apple.com/lookup
        NSArray *array = responseObject[@"results"];
        if (array.count != 0) {// 先判断返回的数据是否为空   没上架的时候是空的
            NSDictionary *dict = array[0];
 
            if ([dict[@"version"] floatValue] > [subVersion floatValue]) {
                //如果有新版本 这里要注意下如果你版本号写得是1.1.1或者1.1.1.1这样的格式,就不能直接转floatValue,自己想办法比较判断。
                UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
                alertWindow.rootViewController = [[UIViewController alloc] init];
                alertWindow.windowLevel = UIWindowLevelAlert + 1;
                [alertWindow makeKeyAndVisible];
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:@"发现新版本。为保证各项功能正常使用,请您尽快更新。" preferredStyle:UIAlertControllerStyleAlert];
                //显示弹出框
                [alertWindow.rootViewController presentViewController:alert animated:YES completion:nil];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"现在更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id123456789?mt=8"]];
                    //这里写的URL地址是该app在app store里面的下载链接地址,其中ID是该app在app store对应的唯一的ID编号。
                    NSLog(@"点击现在升级按钮,跳转");
                }]];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSLog(@"点击下次再说按钮");  //如果不add这段Action,则弹窗中只有1个按钮,即强制用户更新
                }]];
                
            }
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值