1. 程式人生 > >iOS實現應用更新及強制更新

iOS實現應用更新及強制更新

呼叫更新介面返回欄位: result =     {             descr = "";             isupdate = 1;//是否更新             qzupdate = 0;//是否強制更新             updateurl = "
http://www.baidu.com
";//更新地址             versioncode = "2.0";//版本號         };   根據獲取的是否更新、強制更新以及新版本的序號進行判斷 (1)強制更新:無論如何彈出提示框且只有一個選項,點選跳轉更新 (2)普通更新:彈出提示框有“取消”和“確定”兩個選項:點選確定跳轉更新;點選取消本地儲存待更新版本號,再次進入時則和本地儲存的待更新版本號進行判斷,如果相同則彈出提示框,不相同則不操作(例如V1.1版本普通更新選擇”取消”後,後面V1.1的版本不會再次提示,但V1.2版本更新仍會提示) (3)無更新:不操作     本地儲存資料: 待更新的版本序號:@“Version_To_Update"
AppDelegate:
//
檢查版本更新 -(void)checkVersionUpdate{ //檢查更新 NSString *stringVer = [[NSBundle mainBundle] objectForInfoDictionaryKey:(NSString*)kCFBundleVersionKey]; [[NetWorkRequest shareRequest]updateNewVersionWithversioncode:stringVer serverSuccessFn:^(id response) { if ([[response objectForKey:@"
qzupdate"] intValue] == 1 && [[response objectForKey:@"updateurl"] length] > 0) { DebugLog(@"需要強制更新"); NSString *mes = [NSString stringWithFormat:@"發現最新版本%@,需更新後才能繼續使用\n更新內容:%@",[response objectForKey:@"versioncode"],[response objectForKey:@"descr"]]; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil]; alertView.tag = 1001; [alertView show]; }else if ([[response objectForKey:@"isupdate"] intValue] == 1 && [[response objectForKey:@"updateurl"] length] > 0) { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString *version = [userDefaults objectForKey:@"Version_To_Update"];//待更新的版本 version_to_update = [response objectForKey:@"versioncode"]; if ([stringVer floatValue] < [version floatValue] && [version_to_update floatValue] <= [version floatValue]) { //當前待更新版本已點選取消並在本地儲存的待更新版本,不彈出提示框 }else { //彈出提示框進行更新 NSString *mes = [NSString stringWithFormat:@"發現最新版本%@,是否更新?\n更新內容:%@",[response objectForKey:@"versioncode"],[response objectForKey:@"descr"]]; UIAlertView *alertTi = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil]; alertTi.tag = 1002; [alertTi show]; } }else{ //DebugLog(@"不需要更新"); } } serverFailureFn:^(NSError *error, id response) { }]; }