IOS检查版本更新

iOS的版本号, 一个叫做Version, 一个叫做Build.
获得Version ,

[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

获得build号:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

// 以下方法
先获取最新的版本号
再获取当前软件的版本号
最后进行比较

- (void)checkVersionUpdate{

   //同步请求 JSON方法获取数据
    NSError *error;
    NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kAppId];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *resquest = [NSURLRequest requestWithURL:url];
    NSData *response = [NSURLConnection sendSynchronousRequest:resquest returningResponse:nil error:nil];

    NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
    if (error) {
        DLog(@"error: = %@", [error description]);
        return ;
    }

    NSArray *resultArray = [appInfoDic objectForKey:@"results"];
    if (![resultArray count]) {
        DLog(@"error: resultArray  == nil");
        return;
    }

    NSDictionary *infoDic = [resultArray objectAtIndex:0];
    NSString *lastVersion = [infoDic objectForKey:@"version"];
    NSString *trackViewUrl = [infoDic objectForKey:@"trackViewUrl"];
    NSString *trackName = [infoDic objectForKey:@"trackName"];

    //获取此应用的版本号
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDict objectForKey:@" CFBundleShortVersionString"];

    double doubleCurrentVersion = [currentVersion doubleValue];
    double doubleUpdateVersion = [lastVersion doubleValue];

    //比较
    NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@", trackName];
    if (doubleCurrentVersion < doubleUpdateVersion) {
        NSString *messagestr = [NSString stringWithFormat:@"发现新版本:%@, 是否升级?", lastVersion];
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:titleStr message:messagestr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
        alter.tag = [kAppId intValue];
        [alter show];
    }else{
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:titleStr message:@"暂无新版本"delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        alter.tag = [kAppId intValue]+1;
        [alter show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (alertView.tag == [kAppId intValue]) {
        if (buttonIndex == 1) {
            //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:_trackViewUrl]];
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值