每個正在迭代更新的項目,都必不可少的要檢測是否發布了最新的版本,來保證以最優化的版本服務用戶。
其實我并不贊成一打開程序就去檢測,如果這個程序對我來說可有可無,提示我也不會去更新它,我覺的檢測時機可以放在我需要用到這個功能的時候,提示用戶更新,那么我相信,這種更新的幾率就大很多。言歸正傳,我們就談一下具體應該怎么去做。
首先,我們需要知道兩個版本號。
1: 蘋果商店版本(APP Store)
查詢蘋果商店版本,我們首先的有一個地址去查詢
http://itunes.apple.com/lookup?id = 你的應用程序的ID(有9位有10位)
具體怎么發請求,解析數據,這個童鞋們都明白,我們直接看數據
{
resultCount = 1;
results = (
{
advisories = (
);
artistId = 開發者 ID;
artistName = 開發者名稱;
artistViewUrl =
artworkUrl100 =
artworkUrl512 =
artworkUrl60 =
bundleId =
contentAdvisoryRating =
currency =
currentVersionReleaseDate =
description =
features =
fileSizeBytes =
formattedPrice =
genreIds =
genres =
ipadScreenshotUrls =
isGameCenterEnabled =
isVppDeviceBasedLicensingEnabled =
kind =
languageCodesISO2A =
minimumOsVersion =
price =
primaryGenreId =
primaryGenreName =
releaseDate =
releaseNotes = 更新內容
screenshotUrls =
sellerName =
sellerUrl =
supportedDevices =
trackCensoredName =
trackContentRating =
trackId = 應用程序 ID;
trackName =
trackViewUrl =
version = 版本號;
wrapperType =
}
);
}
我們需要的信息一般有 version(版本號) releaseNotes(更新的內容)
2: 正在應用的版本
NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSString* versionNum = [infoDict objectForKey:@"CFBundleVersion"];
我們得到兩個版本之后,剩下的事情就是比較了。
現在版本號一般也都是3位,例如:1.0.0 那么我們就不能轉換為雙精度去比較,我們來試試其他方法:
NSURL *APPUrl = [NSURL URLWithString:GetAppInfoUrl];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLRequest *request = [NSURLRequest requestWithURL:APPUrl];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"APP商店信息-------%@",dic);
int resultCount = [[dic objectForKey:@"resultCount"] intValue];
if(resultCount > 0){
NSArray *infoArray = [dic objectForKey:@"results"];
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *appStoreVersion = [releaseInfo objectForKey:@"version"];
if(infoArray && releaseInfo && appStoreVersion)
{
NSString *note = [releaseInfo objectForKey:@"releaseNotes"];
NSString *localVerson = [Uilities sharedInstance].getCurrentAppVersion;
if ([appStoreVersion compare:localVerson options:NSNumericSearch] == NSOrderedDescending){
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"有新版本了!趕快更新吧,第一時間體驗新功能!"
message:note//更新內容
delegate:self
cancelButtonTitle:@"稍后"
otherButtonTitles:@"更新",nil];
[av show];
});
}
}
}
}];
[task resume];
這里有個點要提醒一下童鞋們,提示框要回到主線程哦!
如果有更新,那么我們就去更新!
// APPDownloadURL : 項目下載地址
NSURL *url = [NSURL URLWithString:APPDownloadURL];
[[UIApplication sharedApplication] openURL:url];