iOS檢查更新并跳轉至AppStore

先上代碼
//1. 在application中調用

-(void)checkVersionUpdata{

    NSString *urlStr    = @"http://itunes.apple.com/lookup?id=1329918420";//id替換即可

    NSURL *url          = [NSURL URLWithString:urlStr];

    NSURLRequest *req   = [NSURLRequest requestWithURL:url];

    [NSURLConnection connectionWithRequest:req delegate:self];

}

//2. 網絡連接

-(void)connection:(NSURLConnection *)connection didReceiveData:(nonnull NSData *)data

{

    NSError *error;

    id jsonObject           = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];

    [XKCEHint disMissLoading];

    NSDictionary *appInfo   = (NSDictionary*)jsonObject;

    NSArray *infoContent    = [appInfo objectForKey:@"results"];

    NSString * version      = [[infoContent objectAtIndex:0]objectForKey:@"version"];//線上最新版本

    // 獲取當前版本

    NSString *currentVersion    = [self version];//當前用戶版本

    BOOL result          = [currentVersion compare:version] == NSOrderedAscending;

    if (result) {//需要更新

        [XKCEGlobleData sharedData].version = @"1";

        NSLog(@"不是最新版本需要更新");

        NSString *updateStr = [NSString stringWithFormat:@"發現新版本V%@\n為保證軟件的正常運行\n請及時更新到最新版本",version];

        [self creatAlterView:updateStr];

    } else {//已經是最新版;

        NSLog(@"最新版本不需要更新");

    }
}

//3. 彈框提示

-(void)creatAlterView:(NSString *)msg{

    UIAlertController *alertText = [UIAlertController alertControllerWithTitle:@"更新提醒" message:msg preferredStyle:UIAlertControllerStyleAlert];

    //增加按鈕

    [alertText addAction:[UIAlertAction actionWithTitle:@"我再想想" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

    }]];

    [alertText addAction:[UIAlertAction actionWithTitle:@"立即更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSString *str = @"itms-apps://itunes.apple.com/cn/app/id1329918420?mt=8"; //更換id即可

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];

    }]];

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

}

//版本

-(NSString *)version

{

    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

    NSString *app_Version       = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

    return app_Version;

}

1. url只要替換成自己的id即可
2. jsonobject中可以獲取到當前軟件在APPstore的信息,返回的內容大致如下
{  
    resultCount = 1;  
    results =     (  
                {  
            artistId = 開發者 ID;  
            artistName = 開發者名稱; 
            price = 0; 
            isGameCenterEnabled = 0;  
            kind = software;  
            languageCodesISO2A =             (  
                EN  
            ); 
            trackCensoredName = 審查名稱;  
            trackContentRating = 評級;  
            trackId = 應用程序 ID;  
            trackName = 應用程序名稱";  
            trackViewUrl = 應用程序介紹網址;  
            userRatingCount = 用戶評級;  
            userRatingCountForCurrentVersion = 1;  
            version = 版本號;  
            wrapperType = software; 
      }  
    );  
}

我們需要的就是版本信息,將當前版本與AppStore獲取的版本相對比,查看是否是最新版本

3. 彈框提示

跳轉到APPstore的url可以直接將id替換。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容