iOS检测版本更新

提示:
检测app版本的更新,必须要获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息。


  • 获取当前运行app版本信息
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

// 版本号:Bundle version
NSString *build = [infoDictionary objectForKey:@"CFBundleVersion"]; 
/*NSString *str=[[NSBundle mainBundle]objectForInfoDictionaryKey:(NSString *)kCFBundleVersionKey]; */ 

//应用标识:Bundle identifier
NSString *bundleId = [infoDictionary objectForKey:@"CFBundleIdentifier"];

//应用名称:Bundle display name
NSString *appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
  • 获取appstore 上发布的最新版本的信息

具体步骤如下:
1,用 POST 方式发送请求:

http://itunes.apple.com/search?term=你的应用程序名称&entity=software

更加精准的做法是根据 app 的 id 来查找:

http://itunes.apple.com/lookup?id=你的应用程序的ID(你的应用程序的ID 是 itunes connect里的 Apple ID)

2,从获得的 response 数据中解析需要的数据。因为从 appstore 查询得到的信息是 JSON 格式的,所以需要经过解析。解析之后得到的原始数据就是如下这个样子的:

{
resultCount = 1;
results = (
{
artistId = 开发者 ID;
artistName = 开发者名称;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
EN
);
trackCensoredName = 审查名称;
trackContentRating = 评级;
trackId = 应用程序 ID;
trackName = 应用程序名称”;
trackViewUrl = 应用程序介绍网址;
userRatingCount = 用户评级;
userRatingCountForCurrentVersion = 1;
version = 版本号;
wrapperType = software;
}
);
}

然后从中取得 results 数组即可,具体代码如下所示:

NSDictionary *jsonData = [dataPayload JSONValue];
NSArray *infoArray = [jsonData objectForKey:@”results”];
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
NSString *latestVersion = [releaseInfo objectForKey:@”version”];
NSString *trackViewUrl = [releaseInfo objectForKey:@”trackViewUrl”];

如果你拷贝 trackViewUrl 的实际地址,然后在浏览器中打开,就会打开你的应用程序在 appstore 中的介绍页面。当然我们也可以在代码中调用 safari 来打开它。
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[NSURL URLWithString:trackViewUrl]];
附:浏览器中打开你的应用程序在 appstore 中的下载介绍页面的另一种URL:

https://itunes.apple.com/cn/app/id你的应用程序的ID?mt=8

代码如下:

导入#import "AFNetworking.h"

#pragma mark - versionRequest----------------------
-(void)onCheckVersion
{    
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];

    NSString *str=[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=1028934662"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        id dict=[NSJSONSerialization  JSONObjectWithData:data options:0 error:nil];

        NSArray *infoArray = [dict objectForKey:@"results"];
        if ([infoArray count]) {

            NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
            NSString *lastVersion = [releaseInfo objectForKey:@"version"];

            if (![lastVersion isEqualToString:currentVersion]) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
                alert.tag          = 10000;
                [alert show];
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alert.tag = 10001;
                [alert show];
            }
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        HLLog(@"发生错误!%@",error);
    }];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==10000) {
        if (buttonIndex==1) {
            NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1028934662?mt=8"];
            [[UIApplication sharedApplication] openURL:url];
        }
    }
}

同此链接:

http://blog.csdn.net/xiaoxuan415315/article/details/9383453

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值