iOS 判断版本号

采用 "https://itunes.apple.com/cn/lookup?id=" + 版本号获取(如果是针对全球上线的用https://itunes.apple.com/lookup?id=) app 信息与本地版本号比较确定是否提示更新.实现代码如下:

/**
 判断 APP 是否有更新

 @param appleid app 在 appstore 的 id 号
 */
- (void)ay_checkAPPVersionWithAppleID:(NSString*)appleid{
    
    NSError *error ;
    NSData *appInfoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@",appleid]] options:NSDataReadingUncached error:&error];
    
    if (!error) {
        NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:appInfoData options:NSJSONReadingMutableContainers error:nil];
        NSString *appInfoVersion = appInfoDic[@"results"][0][@"version"];// APPStore 版本号
        NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
        NSString *ceachVersion = [[NSUserDefaults standardUserDefaults] stringForKey:@"CFBundleShortVersionString"];//沙盒缓存版本号
        if ([self ay_getVersionWithStr:appInfoVersion] > [self ay_getVersionWithStr:currentVersion] && ![ceachVersion isEqualToString:appInfoVersion]) {
            //将 appstore 中的版本号同步到本地
            [[NSUserDefaults standardUserDefaults] setValue:appInfoVersion forKey:@"CFBundleShortVersionString"];
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"更新提示信息" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (iOS10orlater) {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app 下载地址"] options:[NSDictionary dictionary] completionHandler:nil];
                } else {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"app 下载地址"]];
                }
            }];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [alertVC addAction:okAction];
            [alertVC addAction:cancelAction];

            [self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];
        }
    }
}

 

转载于:https://my.oschina.net/AngeloTsui/blog/894789

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值