iOS 自动提示应用需要升级,点击升级跳入App Store下载页

iOS 自动提示应用需要升级,点击升级跳入App Store下载页
1 应用需要打开对应的配置。如下图:
在这里插入图片描述
2 获取应用id
在pc端浏览器打开网址:https://www.apple.com/itunes/,搜索需要的应用在这里插入图片描述
或者直接在App Store上搜索到这个应用,点击分享,复制链接,连接中就有找到字符串id,id后面的数字就是应用id
3 获取App Store上的版本号,跟本地的进行比较,。
//获取APPStore里面的版本号, 可以与本地的版本号作比较,判断是否有新版本更新…

-(BOOL)checkNeedUpdate{
   BOOL needUpdate = NO;
NSString *urlString=@"http://itunes.apple.com/lookup?id=1462858486"; //自己应用在App Store里的地址
NSURL *url = [NSURL URLWithString:urlString];//这个URL地址是该app在iTunes connect里面的相关配置信息。其中id是该app在app store唯一的ID编号。
NSString *jsonResponseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData *data = [jsonResponseString dataUsingEncoding:NSUTF8StringEncoding];//    解析json数据
if (data) {
    id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSArray *array = json[@"results"];
    NSString *newVersion ;
    for (NSDictionary *dic in array) {
        newVersion = [dic valueForKey:@"version"]; // appStore 的版本号
    }
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *appCurVersionNum = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    if (IsNOTEmptyStr(newVersion)) {
        if (newVersion.length > appCurVersionNum.length) {
            //App Store上的长度大于本地的 就更新
            needUpdate = YES;
        }else {
            newVersion = [newVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
            appCurVersionNum = [appCurVersionNum stringByReplacingOccurrencesOfString:@"." withString:@""];
            
        
            /*保证长度一致*/
            while (newVersion.length < appCurVersionNum.length) {
                newVersion =  [newVersion stringByAppendingString:@"0"];
            }
            while (appCurVersionNum.length < newVersion.length) {
                appCurVersionNum =  [appCurVersionNum stringByAppendingString:@"0"];
            }
            
            NSInteger appStoreV = [newVersion  integerValue];
            NSInteger localV = [appCurVersionNum integerValue];
            if (appStoreV > localV) {
                needUpdate = YES;
            }
        }
    }
}

if (needUpdate) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"检查到应用有新版本,是否前去升级?" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"暂不升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id应用id"]];
    }]];
    [self presentViewController:alert animated:YES completion:nil];
}
return needUpdate;

}

注意:
用 “https://itunes.apple.com/lookup?id=” 获取版本号,会出现延迟或请求回来的版本号不稳定还有就是与刚刚发布的版本号对不上。 这个是连接国外的服务器,所以会有延迟。 解决:使用 “https://itunes.apple.com/cn/lookup?id=” 路径。

用"https://itunes.apple.com/lookup?id=" 获取版本号,如果上架的时候只选中国,那么 resultCount 就为 0,获取不到版本数据。解决方法同上。

曾经测试反馈在 APP 刚启动时候都是白屏的情况。发现是版本号请求引起线程阻塞,所以要注意多线程问题。

应用程序还没有发布,如何获取它的 id 啊。不急,App Store Connect 创建要发布应用的时候会生成这个 id 的。

请求中需要的 id,登录开发者账号到 App Store Connect 上查看 App ID。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值