比對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) 

相關文章
相關標籤/搜索