iOS的APP驗(yàn)證版本更新方法

本文網(wǎng)址鏈接:http://www.codes51.com/article/detail_957490_2.html

一個(gè)老項(xiàng)目沒有集成自動(dòng)檢測及提示用戶更新新版本的功能,自己在網(wǎng)上查資料搗鼓出自己的方法,可能比較粗陋,希望大家多多指導(dǎo),我們的項(xiàng)目的版本號(hào)是X.X.X的格式,所以直接把字符串轉(zhuǎn)換成float類型的值無效,自己先將字符串做了處理,后來轉(zhuǎn)成float類型后對版本做比較,以下貼上代碼


#pragma mark - 檢查版本更新
- (void)checkNewVersion {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:APP_URL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) { // 請求成功
        // 字典接收請求到的JSon
        NSDictionary *responseDict = [NSDictionary dictionaryWithDictionary:responseObject];
        // 解析請求到的JSon
        NSArray *results = [responseDict objectForKey:@"results"];
        NSDictionary *finalDict = [results firstObject];
        // 獲取APP下載地址
        NSString *trackViewUrl = [finalDict objectForKey:@"trackViewUrl"];
        updateUrl = [NSURL URLWithString:trackViewUrl];
        // 獲取官網(wǎng)APP的版本號(hào)
        NSString *str1 = [finalDict objectForKey:@"version"];
        NSString *version1 = [str1 substringFromIndex:2];
        // 將版本號(hào)字符串轉(zhuǎn)換成float類型
        float newVersion = [version1 floatValue];
        
        // 獲取本地項(xiàng)目版本號(hào)
        // 拿到項(xiàng)目的infoPlist文件中所有內(nèi)容
        NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
        // 獲取到當(dāng)前工程版本號(hào)
        NSString *str2 = [infoDict objectForKey:@"CFBundleVersion"];
        NSString *version2 = [str2 substringFromIndex:2];
        // 將版本號(hào)字符串轉(zhuǎn)換成float類型
        float localVersion = [version2 floatValue];
        // 對比兩處版本號(hào)
        if (newVersion > localVersion) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"有新版本可用,請安裝更新" delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil];
            alert.tag = 1;
            [alert show];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {// 請求失敗
        // 返回請求失敗的原因
        NSLog(@"NSError:%@", error);
    }];

}

以下是UIAlertView的代理方法,用于監(jiān)聽用戶的點(diǎn)擊后做響應(yīng),跳轉(zhuǎn)到商店下載頁面


#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 1) {
        // 跳轉(zhuǎn)到下載頁面
        [[UIApplication sharedApplication] openURL:updateUrl];
    }
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,663評論 19 139
  • 創(chuàng)建表名 t_student 主鍵為 id CREATE TABLE t_student (id integer...
    mayday2024閱讀 8,524評論 1 10
  • 每當(dāng)我坐在離陽光最近的地方,放好一杯彌漫果香的茶,打開電腦一天的時(shí)候,總會(huì)發(fā)現(xiàn)我并沒有如愿的開始學(xué)學(xué)金融知識(shí)或者找...
    我是小姣閱讀 416評論 2 2

友情鏈接更多精彩內(nèi)容