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

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

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

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

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

获取办法是服务器

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

 

二、复制连接url

 

- (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=8spa

而后将 http:// 替换为 itms:// 或者 itms-apps://线程

替换后的连接地址。blog

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

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

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

相关文章
相关标签/搜索