比对APP当前版本与App Store中的版本是否一致决定是否跳到下载页

苹果官方要求所有的APP不能出现 “当前版本”字样,是因为从iOS8系统开始,你可以在设置里面设置在WiFi情况下,自动更新安装的APP。此功能大大方便了用户,但是一些用户没有开启此项功能,因此还是需要在程序里面提示用户的。方法一是在服务器接口约定对应的数据,这样,服务器直接传递信息,提示用户有新版本,可以去商店升级。方法二是检测手机上安装的APP的版本,然后跟AppStore上app的版本信息联合来判断。

方法一中,之前的做法是在特定的页面的时候 发送请求,根据请求数据,弹出 UIAlertView进行提示。

方法二也是比较常用的方法。
首先,确定安装的APP的版本。

下面的urlStri中 @"https://itunes.apple.com/lookup?id=xxxx" id=后面跟app的商店 ID

获取办法是

1、打开iTunes,右上角在商店搜索你的APP;

2、复制链接

- (void)judgeAPPVersion
{
    
    NSString *urlStr = @"https://itunes.apple.com/lookup?id=1227591538";
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:req delegate:self];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSError *error;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    NSDictionary *appInfo = (NSDictionary *)jsonObject;
    NSArray *infoContent = [appInfo objectForKey:@"results"];
    NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];
    
    NSLog(@"商店的版本是 %@",version);
    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    NSLog(@"当前的版本是 %@",currentVersion);
    
    
    if ([version isEqualToString:currentVersion]) {
        // https://itunes.apple.com/cn/app/%E5%A4%A9e%E9%B2%9C%E4%B9%B0%E5%AE%B6/id1227591538?mt=8
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"商店有最新版本了" message: nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"去商店更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            dispatch_after(0.2, dispatch_get_main_queue(), ^{
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/%E5%A4%A9e%E9%B2%9C%E4%B9%B0%E5%AE%B6/id1227591538?mt=8"]];
            });
        }];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertController addAction:action];
        [alertController addAction:action1];
        [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
    }
    
}

https://itunes.apple.com/cn/app/%E5%A4%A9e%E9%B2%9C%E4%B9%B0%E5%AE%B6/id1227591538?mt=8

然后将 http:// 替换为 itms:// 或者 itms-apps://

替换后的链接地址。

itms-apps://itunes.apple.com/cn/app/%E5%A4%A9e%E9%B2%9C%E4%B9%B0%E5%AE%B6/id1227591538?mt=8

其中用线程去打开App Store是因为会报下面这个错误:

_BSMachError: (os/kern) invalid capability (20)
_BSMachError: (os/kern) invalid name (15) 

原文地址:https://www.cnblogs.com/Walking-Jin/p/6807962.html