每个正在迭代更新的项目,都必不可少的要检测是否发布了最新的版本,来保证以最优化的版本服务用户。
其实我并不赞成一打开程序就去检测,如果这个程序对我来说可有可无,提示我也不会去更新它,我觉的检测时机可以放在我需要用到这个功能的时候,提示用户更新,那么我相信,这种更新的几率就大很多。言归正传,我们就谈一下具体应该怎么去做。
首先,我们需要知道两个版本号。
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];