iOS开发 检测版本更新的实现
NSMutableURLRequest*request=[[NSMutableURLRequestalloc]init];
[requestsetURL:[NSURLURLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]];
[requestsetHTTPMethod:@"GET"];
NSData*returnData=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
NSDictionary*jsonData=[NSJSONSerializationJSONObjectWithData:returnDataoptions:0error:nil];
这里,我们通过同步请求,解析json数据,得到了数据。
NSString*latestVersion=[releaseInfoobjectForKey:@"version"];
NSString*trackViewUrl1 =[releaseInfoobjectForKey:@"trackViewUrl"];//地址trackViewUrl
NSString*trackName=[releaseInfoobjectForKey:@"trackName"];//trackName
获取此应用的版本号
NSString*currentVersion=[infoDictobjectForKey:@"CFBundleVersion"];
通过latestVersion和currentVersion的比较,来判断是否有新的更新。
NSDictionary*infoDict=[[NSBundlemainBundle]infoDictionary];
NSString*currentVersion=[infoDictobjectForKey:@"CFBundleVersion"];
doubledoubleCurrentVersion=[currentVersiondoubleValue];
if(doubleCurrentVersion<doubleUpdateVersion){
UIAlertView*alert;
alert=[[UIAlertViewalloc]initWithTitle:trackName
message:@"有新版本,是否升级!"
delegate: self
cancelButtonTitle:@"取消"
otherButtonTitles: @"升级", nil];
alert.tag = 1001;
[alertshow];
}
else{
UIAlertView*alert;
alert=[[UIAlertViewalloc]initWithTitle:trackName
message:@"暂无新版本"
delegate: nil
cancelButtonTitle:@"好的"
otherButtonTitles: nil, nil];
[alertshow];
}
如果有新的版本,那么就跳转至下载页面,这里就用到了trackViewUrl,trackViewUrl是全路径,直接请求。
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:trackViewUrl]];