苹果给了我们一个接口,能根据应用id请求一些关于应用的信息。我们可以根据返回的信息,来判断版本是否和应用的版本一致,如果不一致,那么就出现新的版本了。这时,就需要向用户提醒有新的版本,需要更新。具体步骤如下:
NSMutableURLRequest*request=[[NSMutableURLRequestalloc]init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]];
[request setHTTPMethod:@"GET"];
NSData*returnData=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
NSDictionary*jsonData=[NSJSONSerializationJSONObjectWithData:returnDataoptions:0error:nil];
NSString*latestVersion=[releaseInfoobjectForKey:@"version"];
NSString*trackViewUrl1 =[releaseInfoobjectForKey:@"trackViewUrl"];//地址trackViewUrl
NSString*trackName=[releaseInfoobjectForKey:@"trackName"];//trackName
获取此应用的版本号
NSString*currentVersion=[infoDictobjectForKey:@"CFBundleVersion"];
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]];
//以"."分隔数字然后分配到不同数组
NSArray * serverArray = [dict[@"version"] componentsSeparatedByString:@"."];
NSArray * localArray = [currentVersion componentsSeparatedByString:@"."];
for (int i = 0; i < serverArray.count; i++) {
if ( [serverArray[i] intValue] > [localArray[i] intValue]) {
//有新版本,提示!提示同上
[self showUpdateAlert];
break;
}
}