老項目沒有加上版本更新提示的功能,新版的app需加上。一下是我寫的版本更新的邏輯:如有版本更新提示,一天(24小時)只提醒一次(方法是記錄上次更新的時間與當前的時間對比)。肯定更新跳轉AppStore更新app,取消當天的更新直接進入系統。
app
(一)判斷是否要更新
code
/** * 是否提示系統更新,判斷的規則:一天內(24小時)只檢測一次系統更新。(默認不提示更新) */ + (void)IsNeed2AppStore { __block BOOL Is2AppStore = NO; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSString *IsNotify = [defaults objectForKey:@"IsNotify"]; NSString *currentDateString = [self formatterDate:[NSDate date]]; //是否首次下載app if (IsNotify) { //是否已保存的更新時間爲當天的時間 if ([IsNotify isEqualToString:currentDateString]) { [self changeUserDefaults:currentDateString Is2AppStore:NO]; }else { AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:kAppStoreUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *lastVersion = [[[responseObject valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"]; if (![kVersion isEqualToString:lastVersion]) { Is2AppStore = YES; } [self changeUserDefaults:currentDateString Is2AppStore:Is2AppStore]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [self changeUserDefaults:currentDateString Is2AppStore:Is2AppStore]; }]; } }else { [self changeUserDefaults:currentDateString Is2AppStore:NO]; } } /** * 修改用戶配置信息 */ + (void)changeUserDefaults:(NSString *)dateString Is2AppStore:(BOOL)Is2AppStore { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:dateString forKey:@"IsNotify"]; [defaults setBool:Is2AppStore forKey:@"Is2AppStore"]; [defaults synchronize]; }
(二)執行更新orm
//更新系統提示 - (void)showNotify { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; BOOL IsUpdate = [defaults boolForKey:@"Is2AppStore"]; //判斷是否須要更新 if (IsUpdate) { [CommonTools showAlertWithUpdateMessage:@"系統有更新" delegate:self]; } } //一天內(24小時)只檢測一次系統更新。(默認不提示更新) - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *dateString = [CommonTools formatterDate:[NSDate date]]; [CommonTools changeUserDefaults:dateString Is2AppStore:NO]; if (buttonIndex == 1) { //更新,跳轉AppStore。 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kAppStoreScheme]]; } }