iOS 版本更新提示

項目需求:
由于公司項目是做離線地圖的,老板希望有版本更新時用戶能及時更新,所以要求app第一次檢測到版本更新時記錄下來這次時間,如果30天后沒更新,app只能使用5次,每次進入app就彈框提示更新和稍后更新,彈出第5次以后,若還未更新則強制用戶更新,不然無法使用app,只有app更新版本后才能繼續(xù)使用

實現(xiàn)思路:
通過聯(lián)網(wǎng)檢測項目在AppStore上的版本號與本地info.plist中獲取的當前版本號對比,
若有新版本則跳轉到AppStore項目頁面下載

實現(xiàn):
從info.plist中獲取項目當前版本號:

NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
NSString *currentVersion=infoDic[@"CFBundleShortVersionString"];

發(fā)送網(wǎng)絡請求獲取App store中項目的最新版本號:
以下方法會發(fā)生請求從app store中檢測app最新的信息,拿到最新的版本號會與info.plist中版本號進行對比判斷,此方法中還封裝了一些業(yè)務邏輯:app在第一次檢測更新到30天以后,會在每次啟動app時提示版本更新和稍后更新,5次以后若還未更新則強制用戶更新,不然無法使用app,
具體根據(jù)公司的要求處理業(yè)務邏輯
下面方法也可以在application:didFinishLaunchingWithOptions:中調用

+ (void)checkAppVersionWithCompletion:(void (^)(BOOL isSuccess))completion {
    
    [[NSUserDefaults standardUserDefaults] setObject:kCurrentVersion forKey:AppUpdateCheckCurrentVersionKey];
    
    // 獲取appStore版本號
    [[[NSURLSession sharedSession] dataTaskWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=953032158"]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (completion) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(data && !error);
            });
        }
        
        if (data == nil || error) {
            return;
        }
        NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
        NSArray *array = [appInfoDic objectForKey:@"results"];
        if (array.count < 1) {
            return;
        }
        
        NSDictionary *dic = [array objectAtIndex:0];
        NSString *appStoreVersion = [dic objectForKey:@"version"];
#if DEBUG
        
        appStoreVersion = @"1000000";
#endif
        BOOL isNewVersion = [kCurrentVersion compare:appStoreVersion options:NSNumericSearch] == NSOrderedAscending;
        if (isNewVersion) {
            
            NSTimeInterval fristCheckAppUpdateTime = [[NSUserDefaults standardUserDefaults] doubleForKey:FristCheckAppUpdateKey];
            NSTimeInterval currentTimeInterval = [[NSDate date] timeIntervalSince1970];
            if (fristCheckAppUpdateTime <= 0.0) {
                // 記錄第一次版本檢測到版本更新的時間
                fristCheckAppUpdateTime = currentTimeInterval;
                [[NSUserDefaults standardUserDefaults] setDouble:fristCheckAppUpdateTime forKey:FristCheckAppUpdateKey];
            }
            
            BOOL isTipUpdate = NO;
            if (!AppUpdateCheckDayCount) {
                isTipUpdate = YES;
            } else {
                NSTimeInterval day30LaterInterval = [NSDate getTimeIntervalWithDayCount:AppUpdateCheckDayCount fromTimeInterval:fristCheckAppUpdateTime];
                // 當?shù)谝淮翁崾靖碌浆F(xiàn)在有30天了,就在每次啟動app時提示用戶更新app
                isTipUpdate = currentTimeInterval >= day30LaterInterval;
            }

            if (isTipUpdate) {
                
                __block NSInteger currentTipCount = [[NSUserDefaults standardUserDefaults] integerForKey:AppUpdateCheckUpdateApkWarnTipCountKey];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    NSString *message = nil;
                    NSArray *buttonTitles = @[];
                    if (currentTipCount >= AppUpdateCheckMaxTipCount) {
                        message = @"程序有最新版本,請更新并運行新版";
                        buttonTitles = @[@"立即更新"];
#if DEBUG
                        buttonTitles = @[@"立即更新", @"Reset"];
#endif
                    } else {
                        message = [NSString stringWithFormat:@"程序有最新版本,是否更新?請注意:您當前的版本還有%ld次使用機會", AppUpdateCheckMaxTipCount-currentTipCount];
                        buttonTitles = @[@"立即更新", @"稍后更新"];
                    }
                    
                    currentTipCount++;
                    [[NSUserDefaults standardUserDefaults] setInteger:currentTipCount forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
                    [[NSUserDefaults standardUserDefaults] synchronize];
                    
                    [UIAlertView showWithTitle:@"有可用版本更新" message:message cancelButtonTitle:nil otherButtonTitles:buttonTitles tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                        if (buttonIndex == 0) {
                            NSURL *url = [NSURL URLWithString:[@"https://itunes.apple.com/us/app/id953032158?ls=1&mt=8" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                            if ([[UIApplication sharedApplication] canOpenURL:url]) {
                                if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f) {
                                    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
                                } else {
                                    [[UIApplication sharedApplication] openURL:url];
                                }
                            }
                        }
#if DEBUG
                        if (buttonIndex == 1 && [buttonTitles containsObject:@"Reset"]) {
                            [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
                            [[NSUserDefaults standardUserDefaults] synchronize];
                        }
#endif
                    }];
                });
            } else {
                [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
                [[NSUserDefaults standardUserDefaults] synchronize];
            }
        } else {
            [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
        
    }] resume];
    
}

當app未更新且提示次數(shù)超過5次時就強制用戶更新,可以在applicationDidBecomeActive中調用下面的方法提示更新,攔截用戶進入app

// 強制更新
+ (void)showUpdateApp {
    
    // 為了防止app更新完成后 啟動時獲取的currentTipCount值沒有改變,會導致再次提示一次強制更新,這里在checkAppVersion時將版本存起來,然后更新完成后再對比就不會出現(xiàn)問題
    NSString *currentVersion = [[NSUserDefaults standardUserDefaults] stringForKey:AppUpdateCheckCurrentVersionKey];
    BOOL isNewVersion = [currentVersion compare:kCurrentVersion options:NSNumericSearch] == NSOrderedAscending;
    // 如果kCurrentVersion大于currentVersion,說明當前版本已經(jīng)更新了
    if (!isNewVersion) {
        // 當app未更新且提示次數(shù)超過5次時就強制用戶更新
        NSInteger currentTipCount = [[NSUserDefaults standardUserDefaults] integerForKey:AppUpdateCheckUpdateApkWarnTipCountKey];
        if (currentTipCount >= AppUpdateCheckMaxTipCount) {
            
            NSArray *buttonTitles = @[@"立即更新"];
#if DEBUG
            buttonTitles = @[@"立即更新", @"Reset"];
#endif
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UIAlertView showWithTitle:@"有可用版本更新" message:@"程序有最新版本,請更新并運行新版" cancelButtonTitle:nil otherButtonTitles:buttonTitles tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                    if (buttonIndex == 0) {
                        NSURL *url = [NSURL URLWithString:[@"https://itunes.apple.com/us/app/id953032158?ls=1&mt=8" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                        if ([[UIApplication sharedApplication] canOpenURL:url]) {
                            if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f) {
                                [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
                            } else {
                                [[UIApplication sharedApplication] openURL:url];
                            }
                        }
                    }
                    
#if DEBUG
                    if (buttonIndex == 1 && [buttonTitles containsObject:@"Reset"]) {
                        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
                        [[NSUserDefaults standardUserDefaults] synchronize];
                    }
#endif
                }];
            });
            
        }
    } else {
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:AppUpdateCheckUpdateApkWarnTipCountKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

Demo

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

推薦閱讀更多精彩內容

  • 最近項目中用到了這個,所以簡要談一下,之前一直沒負責過這塊,順便自己mark一下。 需求:有新版本提示用戶進行...
    捏捏你的臉閱讀 1,160評論 1 4
  • 蘋果審核中如果發(fā)現(xiàn)項目中有版本更新提示,將禁止上架,那么我們可以讓后臺傳一個字段,上架前后修改一下即可,或者通過下...
    fulen閱讀 3,899評論 2 3
  • 在App開發(fā)過程中,難免會碰到類似于添加版本更新提示這種開發(fā)需求。如何更簡單更快捷的實現(xiàn)版本更新提示,便成了重中之...
    SelwynBee閱讀 7,515評論 7 24
  • 用到版本更新,首先現(xiàn)在網(wǎng)絡上搜了搜發(fā)現(xiàn)沒有滿意的,于是參考一個 重寫了一下,支持1.1.1.1.2這種的 版本判斷...
    誰在呼叫賤隊閱讀 1,088評論 0 0
  • 注意:這種方式有延時,會存在如App1.0.1已上線,但是獲取的版本號還是1.0.0。所以還是推薦去后臺記錄版本號...
    程序猿馬國璽閱讀 14,071評論 19 18