方案一:自己維護版本升級
和后臺的同事定義好維護一個版本升級接口,每次發布新版本后在管理平臺更新一下版本信息,移動端通過請求接口獲取最新版本信息來提示用戶是否需要升級。這種方式方便控制是否強制升級等自定義操作。
方案二:檢測蘋果商店版本
有下面幾個請求鏈接: (鏈接中的id:登錄AppStore Connect -> App信息 -> Apple ID;從蘋果商店分享出來的鏈接末尾也是這個id。還有bundleId:填你的App對應的bundleId就行了)
1、https://itunes.apple.com/lookup?id=XXX
2、https://itunes.apple.com/cn/lookup?id=XXX
3、https://itunes.apple.com/lookup?bundleId=com.XXX
4、https://itunes.apple.com/cn/lookup?bundleId=com.XXX
試了下上面這幾個鏈接,使用AFHTTPSessionManager發送POST請求,結果如下:
1、成功,但results目錄下的數據為空
2、成功并返回所有信息
3、成功,但results目錄下的數據為空
4、成功并返回所有信息
請求代碼:
/// 請求接口
- (void)requestAppVersion {
? ? NSString *url = @"https://itunes.apple.com/cn/lookup?bundleId=com.XXX";
? ? AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
? ? [manager POST:url parameters:@{} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id? _Nullable responseObject) {
? ? ? ? NSArray *results = [responseObject objectForKey:@"results"];
? ? ? ? NSDictionary *resultDic = results.firstObject;
? ? ? ? if (resultDic) {
? ? ? ? ? ? [self judgeIfNeedUpdate:resultDic];//判斷是否有新的版本
? ? ? ? }
? ? } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
? ? ? ? NSLog(@"%@",error);
? ? }];
}
/// 判斷是否有新的版本
- (void)judgeIfNeedUpdate:(NSDictionary *)resultDic {
? ? //蘋果商店版本號
? ? NSString *remoteVersion = [resultDic objectForKey:@"version"];
? ? NSArray *remoteArray = [remoteVersion componentsSeparatedByString:@"."];
? ? //本地版本號
? ? NSString *localVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
? ? NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
? ? //版本比較:要分三位依次比較,如果只是單純的去掉版本號中"."再直接比較,有些情況下結果是錯的,例如1.1.0和1.0.10
? ? BOOL ifNeedUpdate = NO;//是否需要更新版本,默認NO
? ? if (remoteArray.count >= 3 && localArray.count >= 3) {
? ? ? ? //比較第一位
? ? ? ? if ([remoteArray[0] integerValue] > [localArray[0] integerValue]) {
? ? ? ? ? ? ifNeedUpdate = YES;
? ? ? ? //第一位相同,比較第二位
? ? ? ? }else if ([remoteArray[0] integerValue] == [localArray[0] integerValue] && [remoteArray[1] integerValue] > [localArray[1] integerValue]) {
? ? ? ? ? ? ifNeedUpdate = YES;
? ? ? ? //第一、二位相同,比較第三位
? ? ? ? }else if ([remoteArray[0] integerValue] == [localArray[0] integerValue] && [remoteArray[1] integerValue] == [localArray[1] integerValue] && [remoteArray[2] integerValue] > [localArray[2] integerValue]) {
? ? ? ? ? ? ifNeedUpdate = YES;
? ? ? ? }
? ? }
? ? //提示用戶有新版本更新,點擊確認后跳轉蘋果商店
? ? if (ifNeedUpdate == YES) {
? ? ? ? UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"更新提示" message:@"您有新的版本可以下載,是否現在去下載!" preferredStyle:UIAlertControllerStyleAlert];
? ? ? ? [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
? ? ? ? }]];
? ? ? ? [alert addAction:[UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
? ? ? ? ? ? NSString *downLoadUrl = [resultDic objectForKey:@"trackViewUrl"];//應用程序下載網址
? ? ? ? ? ? [[UIApplication sharedApplication] openURL:[NSURL URLWithString:downLoadUrl]];//跳轉蘋果商店
? ? ? ? }]];
? ? ? ? [[DTContextGet() navigationController] presentViewController:alert animated:YES completion:nil];
? ? }
? ? //所有數據
? ? //NSString *artistId = resultDic[@"artistId"];? ? ? ? ? ? ? ? ? ? //開發者ID
? ? //NSString *artistName = resultDic[@"artistName"];? ? ? ? ? ? ? ? //開發者名稱
? ? //NSString *releaseDate = resultDic[@"currentVersionReleaseDate"];//最新發布日期
? ? //NSString *minimumOsVersion = resultDic[@"minimumOsVersion"];? ? //最低支持系統版本
? ? //NSString *trackCensoredName = resultDic[@"trackCensoredName"];? //審查名稱
? ? //NSString *trackContentRating = resultDic[@"trackContentRating"];//評級
? ? //NSString *trackId = resultDic[@"trackId"];? ? ? ? ? ? ? ? ? ? ? //應用程序ID
? ? //NSString *trackName = resultDic[@"trackName"];? ? ? ? ? ? ? ? ? //應用程序名稱
? ? //NSString *trackViewUrl = resultDic[@"trackViewUrl"];? ? ? ? ? ? //應用程序下載網址
? ? //NSString *ratingCountAll = resultDic[@"userRatingCount"];? ? ? //用戶評論數量
? ? //NSString *ratingCount = resultDic[@"userRatingCountForCurrentVersion"];//當前版本的用戶評論數量
? ? //NSString *version = resultDic[@"version"];? ? ? ? ? ? ? ? ? ? ? //版本號
? ? //NSArray? *screenshotUrls = resultDic[@"screenshotUrls"];? ? ? ? //App預覽和截屏(可直接打開鏈接查看圖片)
? ? //NSArray? *supportedDevices = resultDic[@"supportedDevices"];? ? //支持的設備型號
}
返回數據格式如下,有用的信息都在results目錄下:
{
? ? "resultCount" : 1,
? ? "results" : [{
? ? ? ? "version" = "版本號"
? ? ? ? "trackViewUrl" = "應用程序下載網址",
? ? }]
}
參考:
如發現遺漏或者錯誤,請在下方評論區留言。