当前运行版本信息可以通过info.plist文件中的bundle version中获取:
- NSDictionary
*infoDic = [[NSBundle mainBundle] infoDictionary]; -
CFShow(infoDic); -
-
NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion"];
这样就获取到当前运行的app的版本了
要获取当前app store上的最新的版本,有两种方法,
一、在某特定的服务器上,发布和存储app最新的版本信息,需要的时候向该服务器请求查询。
二、从app store上查询,可以获取到app的作者,连接,版本等。官方相关文档
www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.htm
具体步骤如下:
1,用 POST 方式发送请求:
http://itunes.apple.com/search?term=你的应用程序名称&entity=software
更加精准的做法是根据 app 的 id 来查找:
http://itunes.apple.com/lookup?id=你的应用程序的ID
#define APP_URL
你的应用程序的ID 是 itunes connect里的 Apple ID
2,从获得的 response 数据中解析需要的数据。因为从 appstore 查询得到的信息是 JSON 格式的,所以需要经过解析。解析之后得到的原始数据就是如下这个样子的:
{
}
然后从中取得 results 数组即可,具体代码如下所示:
NSDictionary *jsonData = [dataPayload JSONValue];
NSArray *infoArray = [jsonData objectForKey:@"results"];
NSDictionary *releaseInfo
NSString *latestVersion = [releaseInfo
NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];
如果你拷贝 trackViewUrl
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[NSURL URLWithString:trackViewUrl]];
- -(void)onCheckVersion:(NSString
*)currentVersion - {
-
NSString *URL = APP_URL; -
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; -
[request setURL:[NSURL URLWithString:URL]]; -
[request setHTTPMethod:@"POST"]; -
NSHTTPURLResponse *urlResponse = nil; -
NSError *error = nil; -
NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; -
-
NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding]; -
NSDictionary *dic = [results JSONValue]; -
NSArray *infoArray = [dic objectForKey:@"results"]; -
if ([infoArray count]) { -
NSDictionary *releaseInfo = [infoArray objectAtIndex:0]; -
NSString *lastVersion = [releaseInfo objectForKey:@"version"]; -
-
if (![lastVersion isEqualToString:currentVersion]) { -
trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"]; -
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil] autorelease]; -
[alert show]; -
} -
} - }