【iOS开放】应用从App Store获取版本信息

来源:互联网 发布:波音787和空客a380知乎 编辑:程序博客网 时间:2024/05/28 18:44

在应用成功上架App Store后,长久的保持对产品的维护和版本升级就是必不可少的流程,我们知道在iOS7以后,用户可以在“设置-> iTunes Store和App Store->更新”中选择打开或关闭应用程序的自动更新,当有网络链接时,系统会不停的将每一个应用的版本信息从iTunes Store或App Store 获取,在有更新时执行更新操作。但是这样一来对用户的网络流量以及iOS设备的使用性能就会对应下降,因此有部分用户还是选择关闭自动更新。

我们所要讨论的就是当用户关闭自动更新,或是当应用有自动更新却还没来得及更新的时候,主动的要求程序向App Store发起请求,查询是否有更新:

- (void)checkAppUpdate {    //向App Store发起请求,APP_ID为应用上架App Store所分配的ID,country参数代表App Store的区域代码,这里以中国为例    NSString *updateUrlString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@&country=%@",APP_ID,@"cn"];        NSURL *updateUrl = [NSURL URLWithString:updateUrlString];    NSMutableURLRequest *versionRequest = [NSMutableURLRequest requestWithURL:updateUrl];    [versionRequest setHTTPMethod:@"GET"];    //同步请求,直接获取NSData    NSData *data = [NSURLConnection sendSynchronousRequest:versionRequest returningResponse:nil error:nil];    if (data) {        NSString *resultStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];        NSLog(@"%@",_appInfo);    }}
这里需要注意的是,如果你的应用不是Global的,在URL里最好带上你country参数,否则就无法正确获取到应用信息。

用三方的JSON库,或是苹果官方的NSJSONSerialization可以将字符串序列化成为NSDictionary,可以看到在result数组中还有这样几个字段

trackId:就是你的AppId

trackName: 应用名称

trackViewUrl:跳转App Store更新的URL

version: 当前应用在App Store最新的版本


通过比较版本号进行提示:

                NSString *latestVersion = [[[_appInfo objectForKey:@"results"] firstObject] objectForKey:@"version"];                if (![APP_VERSION isEqualToString:latestVersion]) {                    //有版本更新                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"有版本更新,是否去更新?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去更新",nil];                    [alert show];                } else {                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"当前版本已为最新!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil,nil];                    [alert show];                }

APP_VERSION 为宏定义:

#define APP_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]


跳转更新

NSString *trackViewUrl = [[[_appInfoobjectForKey:@"results"]firstObject] objectForKey:@"trackViewUrl"];//地址trackViewUrl

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:trackViewUrl]];


ok,搞定!!!



0 0
原创粉丝点击