iOS 检查更新
2017-01-19 本文已影响1125人
胡萝卜须摇头玩
1、从苹果服务器上查询已发布的最新应用版本号
请求的URL地址:
http://itunes.apple.com/lookup?id=你的应用程序的ID
返回的数据为json格式(包括开发者ID,开发者名称,应用ID,应用名称,版本号等):
{
resultCount = 1;
results = (
{
advisories = (
);
appletvScreenshotUrls = (
);
artistId = 1165823318;
artistName = "***";
artistViewUrl = "https://itunes.apple.com/us/developer/***/id1165823318?uo=4";
artworkUrl100 = "http://is2.mzstatic.com/image/thumb/Purple122/v4/77/9b/b2/779bb2af-25ea-405b-23b8-1759617d5b82/source/100x100bb.jpg";
artworkUrl512 = "http://is2.mzstatic.com/image/thumb/Purple122/v4/77/9b/b2/779bb2af-25ea-405b-23b8-1759617d5b82/source/512x512bb.jpg";
artworkUrl60 = "http://is2.mzstatic.com/image/thumb/Purple122/v4/77/9b/b2/779bb2af-25ea-405b-23b8-1759617d5b82/source/60x60bb.jpg";
bundleId = "com.YangtzeUniversity.OilGas";
contentAdvisoryRating = "4+";
currency = USD;
currentVersionReleaseDate = "2017-01-17T20:34:50Z";
description = "\U83b7\U53d6\U6700\U65b0\U6cb9\U6c14\U884c\U4e1a\U8d44\U8baf\U4fe1\U606f\U3002";
features = (
iosUniversal
);
fileSizeBytes = 9250816;
formattedPrice = Free;
genreIds = (
6009,
6002
);
genres = (
News,
Utilities
);
ipadScreenshotUrls = (
"http://a1.mzstatic.com/us/r30/Purple111/v4/7b/3d/34/7b3d3438-7851-8757-7430-b9e9c2866c38/sc1024x768.jpeg",
"http://a1.mzstatic.com/us/r30/Purple111/v4/a2/06/0a/a2060a38-bf91-edcd-a753-1dd6643a3cc4/sc1024x768.jpeg",
"http://a4.mzstatic.com/us/r30/Purple111/v4/0e/08/e9/0e08e988-d000-1642-21ef-b02cb90bcdc3/sc1024x768.jpeg",
"http://a5.mzstatic.com/us/r30/Purple111/v4/00/fa/43/00fa43dc-e04c-87d8-b68e-a0867895d95e/sc1024x768.jpeg"
);
isGameCenterEnabled = 0;
isVppDeviceBasedLicensingEnabled = 1;
kind = software;
languageCodesISO2A = (
EN,
ZH
);
minimumOsVersion = "7.0";
price = 0;
primaryGenreId = 6009;
primaryGenreName = News;
releaseDate = "2016-11-16T19:38:28Z";
releaseNotes = "-\U65b0\U589e\U5728\U5bfc\U822a\U680f\U63d0\U793a\U65b0\U6536\U5230\U7684\U901a\U77e5\U529f\U80fd\Uff1b\n-\U4fee\U590d\U4e0d\U63d0\U793a\U65b0\U804a\U5929\U6d88\U606f\U7684\U95ee\U9898\Uff1b\n-\U4fee\U590d\U65b0\U5efa\U901a\U77e5\U9009\U62e9\U8054\U7cfb\U4eba\U65f6\U53ef\U80fd\U5bfc\U81f4\U8282\U9762\U5361\U987f\U7684\U95ee\U9898\U3002";
screenshotUrls = (
"http://a3.mzstatic.com/us/r30/Purple111/v4/28/7c/7e/287c7ec6-b5d9-d686-dc42-8a603118be4a/screen696x696.jpeg",
"http://a5.mzstatic.com/us/r30/Purple111/v4/40/8b/c4/408bc4bd-6f00-6fa3-26ab-21df7a94e540/screen696x696.jpeg",
"http://a2.mzstatic.com/us/r30/Purple122/v4/55/f8/f7/55f8f76e-2f11-4dc7-6120-0c8508213686/screen696x696.jpeg",
"http://a4.mzstatic.com/us/r30/Purple111/v4/16/8c/95/168c9565-8baf-ba6e-eb4c-f7fd7da3ada8/screen696x696.jpeg",
"http://a5.mzstatic.com/us/r30/Purple111/v4/5c/3c/88/5c3c8812-a9dd-de09-7381-1d5a6192bdaa/screen696x696.jpeg"
);
sellerName = "***";
supportedDevices = (
iPhone4,
iPad2Wifi,
iPad23G,
iPhone4S,
iPadThirdGen,
iPadThirdGen4G,
iPhone5,
iPodTouchFifthGen,
iPadFourthGen,
iPadFourthGen4G,
iPadMini,
iPadMini4G,
iPhone5c,
iPhone5s,
iPhone6,
iPhone6Plus,
iPodTouchSixthGen
);
trackCensoredName = "\U6cb9\U8baf\U901a";
trackContentRating = "4+";
trackId = 1165823319;
trackName = "\U6cb9\U8baf\U901a";
trackViewUrl = "https://itunes.apple.com/us/app/you-xun-tong/id1165823319?mt=8&uo=4";
version = "1.5";
wrapperType = software;
}
);
}
获取其中的版本号:
NSArray *array = responseObject[@"results"];
NSDictionary *dict = [array lastObject];
NSString *serverVersion=dict[@"version"];
2、获取当前应用的版本号
NSString *curVersion=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
3、比较两者,如果服务器上的版本号>当前应用版本号,则提示用户
</br>
最终效果:
86EA7B94742FC0AE15BEB1106E84E9C6.png</br>
附:完整代码
//检查是否有版本更新
+(void)checkForUpdate:(void(^)(void))finishBlock{
NSString *curVersion=[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
NSArray *curVersionArr=[curVersion componentsSeparatedByString:@"."];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//id后面的参数为你的应用程序的ID,此处“1234567890”为实例数据
[manager POST:@"http://itunes.apple.com/lookup?id=1234567890" parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
BOOL hasNewVersion=false;
NSArray *array = responseObject[@"results"];
NSDictionary *dict = [array lastObject];
NSString *serverVersion=dict[@"version"];
NSArray *serverVersionArr=[serverVersion componentsSeparatedByString:@"."];
if (curVersionArr.count>serverVersionArr.count) {
//1.2.1 1.3
for (int i=0; i<serverVersionArr.count; i++) {
int oldVersion=[curVersionArr[i] intValue];
int newVersion=[serverVersionArr[i] intValue];
if (oldVersion<newVersion) {
hasNewVersion=true;
break;
}
}
}else if (curVersionArr.count<serverVersionArr.count) {
//1.2 1.3.1
for (int i=0; i<curVersionArr.count; i++) {
int oldVersion=[curVersionArr[i] intValue];
int newVersion=[serverVersionArr[i] intValue];
if (oldVersion<newVersion) {
hasNewVersion=true;
break;
}
}
}else{
//1.2 1.3
for (int i=0; i<curVersionArr.count; i++) {
int oldVersion=[curVersionArr[i] intValue];
int newVersion=[serverVersionArr[i] intValue];
if (oldVersion<newVersion) {
hasNewVersion=true;
break;
}
}
}
if (hasNewVersion) {
//存在新版本,提示用户更新
finishBlock();
}
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
}];
}