iOS如何取得APP的版本信息跟Apple服务器对比进行升级提示?
ios中应用的版本判断有两种方法:
1.将你的应用版本号同步在你自己的服务器上,打开程序后去自己的服务器获取版本号和手机上的应用版本号做比较,然后去appstore升级
2.通过url获取appstore上的最新版本信息,然后和手机上的程序版本号做比较,判断是否升级。
最常用的就是方法2,下面讲讲方法2的实现过程。
第一步是去获取appstore上你的应用的版本信息,需要用到的url #define APP_URL @"http://itunes.apple.com/lookup?id=662004496"
(替换id即可),我看网上很多的例子都是同步获取信息,这样会阻塞主线程,我还是觉得异步的比较好。
-(void)checkVersion:(NSString* )appurl { ASIFormDataRequest* request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:appurl]]; [request setRequestMethod:@"POST"]; [request setDelegate:self]; [request startAsynchronous]; }
返回结果是需要使用json解析
- (void)requestFinished:(ASIHTTPRequest *)request { NSDictionary* resultDic=[request.responseData JSONValue]; NSArray* infoArray = [resultDic objectForKey:@"results"]; if (infoArray.count>0) { NSDictionary* releaseInfo =[infoArray objectAtIndex:0]; NSString* appStoreVersion = [releaseInfo objectForKey:@"version"]; NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"]; if (![appStoreVersion isEqualToString:currentVersion]) { trackViewURL = [[NSString alloc] initWithString:[releaseInfo objectForKey:@"trackViewUrl"]]; NSString* msg =[releaseInfo objectForKey:@"releaseNotes"]; UIAlertView* alertview =[[UIAlertView alloc] initWithTitle:@"版本升级" message:[NSString stringWithFormat:@"%@%@%@", @"新版本特性:",msg, @"\n是否升级?"] delegate:self cancelButtonTitle:@"稍后升级" otherButtonTitles:@"马上升级", nil]; [alertview show]; } } }
最后一步就是点击升级后的跳转界面
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex==1) { [UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewURL]]; } }
这里要讲讲返回的json数据,aapple返回的数据有很多,你看情况选择你需要的数据,一般需要的一个是version,一个是应用新特性releaseNotes,还有一个就是跳转地址 trackViewUrl
artistId =
artistName =
artistViewUrl =
artworkUrl100 =
artworkUrl512 =
artworkUrl60 =
bundleId =
contentAdvisoryRating =
currency =
description =
features =
fileSizeBytes =
formattedPrice =
genreIds =
genres =
ipadScreenshotUrls =
isGameCenterEnabled =
kind =
languageCodesISO2A =
price = 0;
primaryGenreId = 6017;
primaryGenreName =
releaseDate = "2013-08-07T07:03:16Z";
releaseNotes = "1\U3001\U6dfb\U52a0QQ\U8054\U7cfb\U529f\U80fd\n2\U3001\U8bfe\U7a0b\U81ea\U52a8\U64ad\U653e\U65f6\U6e10\U9690\U8fc7\U5ea6\Uff0c\U89c6\U89c9\U6548\U679c\U66f4\U597d\n3\U3001\U4e13\U9898\U6392\U7248\U91cd\U65b0\U8bbe\U8ba1\Uff0c\U4f7f\U4e4b\U66f4\U591a\U7684\U663e\U793a\U4e13\U9898\U4ecb\U7ecd\n4\U3001\U8f6e\U64ad\U56fe\U7247\U52a0\U70b9\U6307\U793a\n5\U3001\U5b66\U9662\U4ecb\U7ecd\U53d8\U6210\U5bcc\U6587\U672c\Uff0c\U53ef\U4ee5\U6d4f\U89c8\U5230\U56fe\U7247\U63cf\U8ff0\n6\U3001\U4fee\U6539\U4e00\U4e9bbug";
screenshotUrls =
sellerName =
supportedDevices =
trackCensoredName =
trackContentRating =
trackId =
trackName =
trackViewUrl = "https://itunes.apple.com/us/app/quan-min-yu-jia-xue-xiao/id662004496?mt=8&uo=4";
version = "1.0.1";
wrapperType =