本文的目的就一个,通过对比 App Store 成功发布的App 版本号 来确认当前手机中安装的客户端版本号是否有区别.
如果有区别,那么可以提示用户可以去App Store 升级了.
首先是获取App Store 某个应用的版本号,提供App ID 即可,返回Json形式的字符串
http://itunes.apple.com/lookup?id=507704613
接下来通过 NSMutableURLRequest 和NSURLConnection 访问上面的Url 获取Json串,代码如下:
NSString *post=nil; post=[[NSString alloc]initWithFormat:@"id=%@",_appID];//testid为你的appid NSData *postData=[post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength=[NSString stringWithFormat:@"%d",[postData length]]; NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init]; [request setURL:[NSURL URLWithString:@"http://itunes.apple.com/lookup?"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSURLConnection *conn=[[NSURLConnection alloc]initWithRequest:request delegate:self]; [request release]; [conn release]; [post release];
执行成功后,委托获取Json数据. 此时通过 SBJson开源框架 将 Json 字符串 转换成 NSDictionary转换之前先将 获取到的 NSData
转换成 NSString
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [_appInfoData appendData:data]; } -(void) connectionDidFinishLoading:(NSURLConnection *)connection { NSString *jsonString=[[NSString alloc] initWithData:_appInfoData encoding:NSUTF8StringEncoding]; SBJsonParser *jsonParser = [[SBJsonParser alloc] init]; NSDictionary *jsonData = (NSDictionary *)[jsonParser objectWithString:jsonString error:NULL]; }
将NSDictionary打印出如下结构数据
{
resultCount = 1;
results =(
{
artistId =公司 ID;
artistName = "公司名称";
artistViewUrl = "公司地址,可以看到本公司发布的其他App";
artworkUrl100 = "大版的App ICON 1024*1024";
artworkUrl512 = "大版的App ICON 1024*1024";
artworkUrl60 = "小版的App ICON 57*57";
bundleId = "com.flyrise.FEOA";//App Identifier
contentAdvisoryRating = "4+";
currency = USD;
description = "描述";
features = ();
fileSizeBytes = 4166039;
formattedPrice = Free;
genreIds = (
6000,
6015
);
genres = (
Business,
Finance
);
ipadScreenshotUrls = (
);
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
ZH
);
price = 0;
primaryGenreId = 6000;
primaryGenreName = Business;
releaseDate = "2012-05-18T10:36:58Z";
releaseNotes = "新版本发布描述";
screenshotUrls =(
"介绍图片1",
"介绍图片2",
"介绍图片3",
"介绍图片4",
"介绍图片5"
);
sellerName = "ZHUHAI FLYRISE Co.,LTD";
supportedDevices = (
all
);
trackCensoredName = "App 名称";
trackContentRating = "4+";
trackId =App ID;
trackName = "App 名称";
trackViewUrl = "http://itunes.apple.com/us/app/ye-wu-xie-zuo-ping-tai/id507704613?mt=8&uo=4";//App
Store 某个App 的地址
version = "5.5.1";//App 当前版本号,进行版本对比的重点
wrapperType = software;
}
);
}
通过转换后的NSDictionary 获取App Store发布成功的App 版本号
NSArray *infoArrays=[jsonData objectForKey:@"results"]; NSDictionary *releaseInfo=[infoArrays objectAtIndex:0]; NSString *releaseVersion =[releaseInfo objectForKey:@"version"];
获取手机已经安装的App 版本号
NSString *localVersion =[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
那么进行字符串对比,就可以知道需不需要更新了.