iOS app升級(jí)提醒及跳轉(zhuǎn)AppStore

今天研究了一下app升級(jí)提示的流程,不難很簡(jiǎn)單。希望小白們能用得上。。。

<UIAlertViewDelegate>
/**
 *   判斷app安裝版本和商店版本的比較
 *
 *    PS:還是介紹一下對(duì)應(yīng)信息
 *    trackCensoredName = 審查名稱(chēng);
 *    trackContentRating = 評(píng)級(jí);
 *    trackId = 應(yīng)用程序 ID;
 *    trackName = 應(yīng)用程序名稱(chēng);
 *    trackViewUrl = 應(yīng)用程序介紹網(wǎng)址;
 *    userRatingCount = 用戶(hù)評(píng)級(jí);
 *    userRatingCountForCurrentVersion = 1;
 *    version = 版本號(hào);
 */
-(void)judgeAPPVersion
{
    // 快捷方式獲得session對(duì)象
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/lookup?id=1048992038"];
    // 通過(guò)URL初始化task,在block內(nèi)部可以直接對(duì)返回的數(shù)據(jù)進(jìn)行處理
    NSURLSessionTask *task = [session dataTaskWithURL:url
                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                        NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]);
                                        
                                        NSError *errorJs;
                                        id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&errorJs];
                                        NSDictionary *appInfo = (NSDictionary *)jsonObject;
                                        NSArray *infoContent = [appInfo objectForKey:@"results"];
                                        NSString *version = [[infoContent objectAtIndex:0] objectForKey:@"version"];
                                        
                                        NSLog(@"商店的版本是 %@",version);

                                        NSString *skipUrl = [[infoContent objectAtIndex:0] objectForKey:@"trackViewUrl"];
                                        
                                        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
                                        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
                                        NSLog(@"當(dāng)前的版本是 %@",currentVersion);
                                        
                                        if (![version isEqualToString:currentVersion]) {
                                            // 跳轉(zhuǎn)appstore
                                            [self skipAppStore:skipUrl];
                                        }
                                        
                                    }];
    
    // 啟動(dòng)任務(wù)
    [task resume];
}

- (void)skipAppStore:(NSString*)strSkipUrl{
    // 初始化
    UIAlertController *alertDialog = [UIAlertController alertControllerWithTitle:@"是否更新新版本" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    // 分別創(chuàng)建操作
    UIAlertAction *laterAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        // App 跳轉(zhuǎn)
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strSkipUrl]];
    }];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        // 取消按鍵
        
    }];
    
    // 添加操作(順序就是呈現(xiàn)的上下順序)
    [alertDialog addAction:laterAction];
    [alertDialog addAction:okAction];
    
    // 呈現(xiàn)警告視圖
    [self presentViewController:alertDialog animated:YES completion:nil];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,117評(píng)論 25 708
  • 我在與你相距遙遠(yuǎn)的南方和你聊天,忽然你說(shuō),我喜歡你,你沒(méi)有發(fā)現(xiàn)嗎?這不禁讓我內(nèi)心一震,想起了和你的許多事情,都變得...
    白康南閱讀 761評(píng)論 0 0
  • #悅讀悅美,書(shū)香校園#初讀《塵埃落定》我只覺(jué)得這是本有趣的書(shū),腦子里的印象只有傻子,聰明人。而到再讀一遍,我開(kāi)...
    婷婷我有少女心閱讀 377評(píng)論 0 0
  • 成功不是人生的終極目標(biāo),讓自己變的更好才是。 之前一直在糾結(jié)人活著到底是為什么,人生的意義到底是什么,但卻終日不得...
    光年123閱讀 250評(píng)論 0 1
  • 之前在后臺(tái)收到一個(gè)女孩的消息問(wèn)我說(shuō):最近有個(gè)還不錯(cuò)的男生追我,我也挺喜歡他的,但是吧,又覺(jué)得我們還沒(méi)畢業(yè),不知道能...
    茗藝堂閱讀 548評(píng)論 0 1