App Store自动更新App oc

苹果App Store可以帮忙管理版本,只要在登录或起始页面加上下面代码就可以实现自动更新App版本。

检查当前版本是否需要更新:

    //获取手机程序的版本号

    NSString *ver = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

    NSDictionary *dict = @{@"id":@"1268501964"};//在开发者账号中查看

    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];

    [mgr.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil]];

    [mgr GET:@"https://itunes.apple.com/cn/lookup" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSArray *array = responseObject[@"results"];

        if (array.count != 0) {// 先判断返回的数据是否为空

            NSDictionary *dict = array[0];

            // 当前版本小于App Store版本

            if ([dict[@"version"] compare:ver options:NSNumericSearch] == NSOrderedDescending) {

                // 提示更新

                if (!updateAlert.visible) {

                    [updateAlert show];

                }

            }

        }

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];

更新App:

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/%E8%8E%B1%E4%BB%98mpos/id1268501964?mt=8"]];//替换id

猜你喜欢

转载自blog.csdn.net/weixin_42012181/article/details/86569130