自動檢測更新實現邏輯:git
先上github地址:https://github.com/wolfhous/HSUpdateAppgithub
1,獲取當前項目APP版本號網絡
2,拿到AppStore項目版本號app
3,對比版本號,實現更新功能google
一點源碼:url
1 /** 2 * 天朝專用檢測app更新 3 */ 4 -(void)hsUpdateApp 5 { 6 //2先獲取當前工程項目版本號 7 NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary]; 8 NSString *currentVersion=infoDic[@"CFBundleShortVersionString"]; 9 10 //3從網絡獲取appStore版本號 11 NSError *error; 12 NSData *response = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",STOREAPPID]]] returningResponse:nil error:nil]; 13 if (response == nil) { 14 NSLog(@"你沒有鏈接網絡哦"); 15 return; 16 } 17 NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; 18 if (error) { 19 NSLog(@"hsUpdateAppError:%@",error); 20 return; 21 } 22 // NSLog(@"%@",appInfoDic); 23 NSArray *array = appInfoDic[@"results"]; 24 NSDictionary *dic = array[0]; 25 NSString *appStoreVersion = dic[@"version"]; 26 //打印版本號 27 NSLog(@"當前版本號:%@\n商店版本號:%@",currentVersion,appStoreVersion); 28 //4當前版本號小於商店版本號,就更新 29 if([currentVersion floatValue] < [appStoreVersion floatValue]) 30 { 31 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"版本有更新" message:[NSString stringWithFormat:@"檢測到新版本(%@),是否更新?",appStoreVersion] delegate:self cancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil]; 32 [alert show]; 33 }else{ 34 NSLog(@"版本號好像比商店大噢!檢測到不須要更新"); 35 } 36 37 }
1 - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 2 { 3 //5實現跳轉到應用商店進行更新 4 if(buttonIndex==1) 5 { 6 //6此處加入應用在app store的地址,方便用戶去更新,一種實現方式以下: 7 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/us/app/id%@?ls=1&mt=8", STOREAPPID]]; 8 [[UIApplication sharedApplication] openURL:url]; 9 } 10 }
而後在你想檢測更新的窗口實現:spa
1 //一句代碼實現檢測更新 2 [self hsUpdateApp];
附:STOREAPPID這個宏定義爲你的app在商店的id號,就是一串數字,一種方法是本身登錄itunesconnect能夠查看,另外一種方法是百度或者googlecode