iOS检测更新

419 阅读1分钟

##原理: ###通过appid访问 http://itunes.apple.com/lookup 可以获取appStore上本app最新版本的信息然后和当前安装的版本做比较, 如果本地低于最新版本弹框提示让用户选择是否跳转到appstore更新。

##获取所安装的app版本号

    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    _LocationVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];

##获取appStore最新版本号 并且比较两者 ###示例只取最新的版本号。更多信息可以通过控制台的输出得到获取字段。

    AFWeakSelf(self);
    [[NetKit kit] GET:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APP_ID] parameters:nil success:^(id ret) {
        AFStrongSelf(self);
        NSLog(@"最新版本信息%@",ret);
        if ([ret hasObjectWithKey:@"results"]) {
            NSArray *infoArray = ret[@"results"];
            if (infoArray.count) {
                NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
                NSString *lastVersion = [releaseInfo objectForKey:@"version"];
                
                if ([_LocationVersion compare:lastVersion options:NSNumericSearch] == NSOrderedAscending)
                {
                    [UIAlertView bk_showAlertViewWithTitle:@"更新提示" message:@"发现新版本,是否立即更新?" cancelButtonTitle:@"立即更新" otherButtonTitles:@[@"以后再说"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                        if (buttonIndex == 0) {
                            [self PushtoAppStore:releaseInfo[@"trackViewUrl"]];
                        }
                    }];
                }
                
            }
            
        }
        
    } failure:^(NSError *error) {
        
    }];

##跳转到appstore下载

- (void)PushtoAppStore:(NSString *)trackViewUrl{
    NSURL *url = [NSURL URLWithString:APP_LINK];
    [[UIApplication sharedApplication] openURL:url];
}

##有先行者已经写好了封装 AYCheckVersion