實現:數組
強制更新:每次彈框app
非強制更新:一天提示一次url
代碼以下:spa
步驟一: 將檢測更新寫到APPDelegate的applicationDidBecomeActive中code
步驟二: 檢測是否須要更新component
步驟三: 針對非強制更新-首先判斷日期若是是同一天的話就不提示更新,若是不是同一天能夠提示更新orm
邏輯以下:前邊是以前的邏輯 簡單解釋一下:0表明未提示更新 1表明已經提示更新 存日期表示將日期存到內存中blog
前邊是第一次的邏輯,後邊是寫代碼時候的邏輯,更簡化了一些內存
下邊附上核心代碼:字符串
- (void)compareVersionLocalVerson:(NSString *)localVerson appVerson:(NSString *)appVerson andtype:(NSInteger)type andURl:(NSString *)url{ //將版本號按照.切割後存入數組中 NSArray *localArray = [localVerson componentsSeparatedByString:@"."]; NSArray *appArray = [appVerson componentsSeparatedByString:@"."]; NSInteger minArrayLength = MIN(localArray.count, appArray.count); BOOL needUpdate = NO; for(int i=0;i<minArrayLength;i++){//以最短的數組長度爲遍歷次數,防止數組越界 //取出每一個部分的字符串值,比較數值大小 NSString *localElement = localArray[i]; NSString *appElement = appArray[i]; NSInteger localValue = localElement.integerValue; NSInteger appValue = appElement.integerValue; if(localValue<appValue) { //從前日後比較數字大小,一旦分出大小,跳出循環 needUpdate = YES; break; }else if(localValue>appValue){ needUpdate = NO; break; } } if (needUpdate) { if (type == 1) {//強制更新 [self showForceUpdate]; }else{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSString *nowday = [formatter stringFromDate:[NSDate date]]; NSString *saveDay = [UserDefaults objectForKey:@"versionUpdateKey"]; if (saveDay == nil) { saveDay = @""; } if (![saveDay isEqualToString:nowday]) { //假如不是同一天,更新存儲的日期,而且把isHadShowUpdate 設置成yes [self canChooseUpdate]; [UserDefaults setObject:@"1" forKey:@"isHadShowUpdate"]; [UserDefaults setObject:nowday forKey:@"versionUpdateKey"]; }else{//若是是同一天的話 return; // if([IsHadShowUpdate isEqualToString:@"0"]){ // [self canChooseUpdate]; // [UserDefaults setObject:@"1" forKey:@"isHadShowUpdate"]; // [UserDefaults setObject:nowday forKey:@"versionUpdateKey"]; // }else{ // return; // } } } }else{ } }
非強制更新代碼
//可選更新 -(void)canChooseUpdate{ //彈出提示更新彈框 UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"親,有新版本了" message:@"更穩定、快速、多彩的功能和體驗,點擊當即更新!" preferredStyle:UIAlertControllerStyleAlert]; // UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSString *JumpURL = [[NSUserDefaults standardUserDefaults]objectForKey:@"AppURL"]; if(JumpURL.length ==0){ [JKToast showWithText:@"參數錯誤"]; return; }else{ [[UIApplication sharedApplication] openURL:[NSURL URLWithString:JumpURL]]; AppDelegate *app = appDelegate; UIWindow *window = app.window; [UIView animateWithDuration:1.0f animations:^{ window.alpha = 0; window.frame = CGRectMake(0, window.bounds.size.width, 0, 0); } completion:^(BOOL finished) { exit(0); }]; } }]; [alertVc addAction:action2]; [alertVc addAction:action1]; UIViewController *vc = [UIApplication sharedApplication].delegate.window.rootViewController; [vc presentViewController:alertVc animated:YES completion:nil]; }