ios 实现版本更新检查
注:这里网络请求用的是第三方框架:SVHTTPRequest
/*
第一步: 根据应用名称搜索应用,然后根据应用绑定的ID在结果中筛选出我们要找的应用,并取出应用的AppID
*/
- (void)getAppID {
processView = [[UIProcessViewalloc]initWithFrame:self.view.framewithText:@"检测中..."];
[processViewstartAnimating];
[self.viewaddSubview:processView];
[SVHTTPRequestPOST:@"http://itunes.apple.com/search"
parameters:[[NSDictionaryalloc] initWithObjectsAndKeys:APP_NAME,@"term",@"software",@"entity",nil]
completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error&&[urlResponse statusCode]==200) {
NSData *data = (NSData *)response;
id res = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];
NSLog(@"res.class==%@",[res class]);
NSLog(@"res == %@",res);
NSLog(@"results class == %@",[[res objectForKey:@"results"]class]);
NSArray *arr = [res objectForKey:@"results"];
for (id config in arr)
{
NSString *bundle_id = [config valueForKey:@"bundleId"];
if ([bundle_id isEqualToString:APP_BUNDLE_IDENTIFIER]) {
[processView stopAnimating];
app_id = [config valueForKey:@"trackId"];
updateURL = [config valueForKey:@"trackViewUrl"];
NSString *app_Name = [config valueForKey:@"trackName"];
NSString *version = [config valueForKey:@"version"];
NSLog(@"app_id == %@,app_Name == %@,version == %@",app_id,app_Name,version);
[self checkUpdate:version];
}
}
} else {
[processView stopAnimating];
[CTCommonUtilsshowAlertViewOnView:self.viewwithText:@"检测失败,当前无网络连接!"];
}
}];
}
/*
第二步:通过比较从App Store获取的应用版本与当前程序中设定的版本是否一致,然后判断版本是否有更新
*/
- (void)checkUpdate:(NSString *)versionFromAppStroe {
NSDictionary *infoDict = [[NSBundlemainBundle] infoDictionary];
NSString *nowVersion = [infoDict objectForKey:@"CFBundleVersion"];
NSLog(@"nowVersion == %@",nowVersion);
[processViewstopAnimating];
//检查当前版本与appstore的版本是否一致
if (![versionFromAppStroe isEqualToString:nowVersion])
{
UIAlertView *createUserResponseAlert = [[UIAlertView alloc] initWithTitle:@"提示" message: @"有新的版本可供下载" delegate:self cancelButtonTitle:@"下次再说" otherButtonTitles: @"去下载", nil];
[createUserResponseAlert show];
} else {
[CTCommonUtilsshowAlertViewOnView:self.viewwithText:@"暂无新版本"];
}
}
#pragma mark - AertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1) {
//去appstore中更新
//方法一:根据应用的id打开appstore,并跳转到应用下载页面
//NSString *appStoreLink = [NSString stringWithFormat:@"http://itunes.apple.com/cn/app/id%@",app_id];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStoreLink]];
//方法二:直接通过获取到的url打开应用在appstore,并跳转到应用下载页面
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:updateURL]];
} else if (buttonIndex == 2) {
//去itunes中更新
[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"itms://itunes.apple.com/cn/app/guang-dian-bi-zhi/id511587202?mt=8"]];
}
}
#pragma mark -