標籤:ios自動檢測版本更新 ios應用自動檢測版本更新 iosapp自動檢測版本更新
雖然蘋果官方是不允許應用自動檢測更新,提示使用者下載,因為蘋果會提示你有多少個軟體需要更新,但是有的時候提示使用者一下有新版還是很有必要的。
首先說一下原理:
每個上架的蘋果應用程式,都會有一個應用程式的ID,根據這個ID我們就可以擷取到當前程式的最新版本號碼,然後和自己的版本號碼作比較,如果一樣的話就是最新版,反之就不是新版,就可以提示使用者來手動下載最新版的程式。因為有ID所以就可以定位到這個APP,點擊下載即可。
源碼:
一般建議檢測更新的代碼放到首頁控制器裡。
首先還要匯入一個標頭檔用來開啟AppStore下載更新
//AppStore#import <StoreKit/StoreKit.h>
接著還有代理
SKStoreProductViewControllerDelegate
然後開始檢測更新
//檢測版本,版本更新 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]; }
接著就是使用者更不更的問題了
- (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是不是最新版了,而且使用者也能即時看到,最關鍵的是蘋果審核還能通過。
本文出自 “紅角羚羊” 部落格,請務必保留此出處http://2254359459.blog.51cto.com/10776102/1876839
iOS自動檢測版本更新