iOS版本更新提示

注意:這種方式有延時(shí),會(huì)存在如App1.0.1已上線,但是獲取的版本號(hào)還是1.0.0。所以還是推薦去后臺(tái)記錄版本號(hào)

iOS更新提示比較簡(jiǎn)單,不需要后臺(tái)記錄版本號(hào),直接去App Store獲取最新版本即可。

NSString * url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",AppID];//替換為自己App的ID
// 獲取本地版本號(hào)
NSString * currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
// 網(wǎng)絡(luò)請(qǐng)求獲取最新版本
[[HttpBaseManager getSharedManager] GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray * results = responseObject[@"results"];
        if (results && results.count>0)
        {
            NSDictionary * dic = results.firstObject;
            NSString * lineVersion = dic[@"version"];//版本號(hào)
            NSString * releaseNotes = dic[@"releaseNotes"];//更新說(shuō)明
            //NSString * trackViewUrl = dic[@"trackViewUrl"];//鏈接
            //把版本號(hào)轉(zhuǎn)換成數(shù)值
            NSArray * array1 = [currentVersion componentsSeparatedByString:@"."];
            NSInteger currentVersionInt = 0;
            if (array1.count == 3)//默認(rèn)版本號(hào)1.0.0類(lèi)型
            {
                currentVersionInt = [array1[0] integerValue]*100 + [array1[1] integerValue]*10 + [array1[2] integerValue];
            }
            NSArray * array2 = [lineVersion componentsSeparatedByString:@"."];
            NSInteger lineVersionInt = 0;
            if (array2.count == 3)
            {
                lineVersionInt = [array2[0] integerValue]*100 + [array2[1] integerValue]*10 + [array2[2] integerValue];
            }
            if (lineVersionInt > currentVersionInt)//線上版本大于本地版本
            {
                UIAlertController * alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"發(fā)現(xiàn)新版本%@",lineVersion] message:releaseNotes preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction * ok = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
                UIAlertAction * update = [UIAlertAction actionWithTitle:@"去更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    //跳轉(zhuǎn)到App Store
                    NSString *urlStr = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8",AppID];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];
                }];
                [alert addAction:ok];
                [alert addAction:update];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"版本更新出錯(cuò),%@",error.description);
    }];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 項(xiàng)目需求:由于公司項(xiàng)目是做離線地圖的,老板希望有版本更新時(shí)用戶(hù)能及時(shí)更新,所以要求app第一次檢測(cè)到版本更新時(shí)記錄...
    字節(jié)碼閱讀 989評(píng)論 3 2
  • 最近項(xiàng)目中用到了這個(gè),所以簡(jiǎn)要談一下,之前一直沒(méi)負(fù)責(zé)過(guò)這塊,順便自己mark一下。 需求:有新版本提示用戶(hù)進(jìn)行...
    捏捏你的臉閱讀 1,160評(píng)論 1 4
  • 東野圭吾的同名小說(shuō)已經(jīng)有日版韓版 國(guó)產(chǎn)版 蘇有朋導(dǎo)演的這部電影聽(tīng)說(shuō)劇本改了36次之多 終于在前幾天上映 由于時(shí)間地...
    烏鴉吃蛋糕閱讀 478評(píng)論 8 2
  • 2017年已然過(guò)去一半,內(nèi)心明確了兩個(gè)點(diǎn):1.無(wú)論是從那個(gè)角度出發(fā),中醫(yī)這條路作為愛(ài)好,終將發(fā)展成我的特長(zhǎng),也是我...
    觀宇閱讀 222評(píng)論 0 0
  • 說(shuō)到西游記,大家應(yīng)該都不陌生,這本書(shū)講述了唐僧師徒四人西天取經(jīng)的故事,然而,作為我國(guó)四大古典名著之一,西游記又顯得...
    追風(fēng)者我思我在閱讀 476評(píng)論 0 3