方法一中,以前的作法是在特定的頁面的時候 發送請求,根據請求數據,彈出 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)