雖然蘋果官方是不容許應用自動檢測更新,提示用戶下載,由於蘋果會提示你有多少個軟件須要更新,可是有的時候提示用戶一下有新版仍是頗有必要的。服務器
首先說一下原理:app
每一個上架的蘋果應用程序,都會有一個應用程序的ID,根據這個ID咱們就能夠獲取到當前程序的最新版本號,而後和本身的版本號做比較,若是同樣的話就是最新版,反之就不是新版,就能夠提示用戶來手動下載最新版的程序。由於有ID因此就能夠定位到這個APP,點擊下載便可。url
源碼:spa
通常建議檢測更新的代碼放到主頁控制器裏。代理
首先還要導入一個頭文件用來打開AppStore下載更新code
//AppStore #import <StoreKit/StoreKit.h>
接着還有代理component
SKStoreProductViewControllerDelegate
而後開始檢測更新orm
//檢測版本,版本更新 NSError *error; NSString *urlStr = @"http://itunes.apple.com/lookup?id=上架AppID"; NSURL *url = [NSURL URLWithString:urlStr]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error]; if (error) { return; } NSArray *resultArray = [appInfoDict objectForKey:@"results"]; if (![resultArray count]) { return; } NSDictionary *infoDict = [resultArray objectAtIndex:0]; //獲取服務器上應用的最新版本號 NSArray* arr=[infoDict[@"version"] componentsSeparatedByString:@"."]; NSInteger updateVersion=0; for (int i=0; i<arr.count; i++) { if(i==0) { updateVersion+=[arr[i] integerValue]*1000; } else if (i==1) { updateVersion+=[arr[i] integerValue]*100; } else if (i==2) { updateVersion+=[arr[i] integerValue]*10; } else if (i==3) { updateVersion+=[arr[i] integerValue]*1; } } NSString *trackName = infoDict[@"trackName"]; _trackViewUrl = infoDict[@"trackViewUrl"]; //獲取當前設備中應用的版本號 NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSArray* arr2=[[infoDic objectForKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."]; NSInteger currentVersion=0; for (int i=0; i<arr2.count; i++) { if(i==0) { currentVersion+=[arr2[i] integerValue]*1000; } else if (i==1) { currentVersion+=[arr2[i] integerValue]*100; } else if (i==2) { currentVersion+=[arr2[i] integerValue]*10; } else if (i==3) { currentVersion+=[arr2[i] integerValue]*1; } } //判斷兩個版本是否相同 if (currentVersion < updateVersion) { NSString *titleStr = [NSString stringWithFormat:@"檢查更新:%@", trackName]; NSString *messageStr = [NSString stringWithFormat:@"發現新版本%@,是否更新", infoDict[@"version"]]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升級", nil];
//爲了區分其餘彈出框而已 alert.tag = 1112427256; [alert show]; }
接着就是用戶更不更的問題了blog
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 1112427256) { if (buttonIndex == 1) { //點擊」升級「按鈕,就從打開app store上應用的詳情頁面 SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init]; storeProductVC.delegate = self; NSDictionary *dict = [NSDictionary dictionaryWithObject:@"上架AppID" forKey:SKStoreProductParameterITunesItemIdentifier]; [storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error) { if (result) { [self presentViewController:storeProductVC animated:YES completion:nil]; } }]; } } }
還有就是用戶打開AppStore可是沒有下載就返回回來的情況源碼
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController { [viewController dismissViewControllerAnimated:YES completion:nil]; }
OK到這裏就結束了。這樣的話就能夠檢測App是否是最新版了,並且用戶也能實時看到,最關鍵的是蘋果審覈還能經過。