您的位置:首页 > 移动开发 > IOS开发

ios检测新版本方法并更新

2015-02-02 16:32 197 查看
//检查版本更新

//APPLEID 是提交到appstore的应用id
-(void)onCheckVersion
{

NSDictionary *infoDic = [[NSBundlemainBundle]
infoDictionary];
NSString *currentVersion = [infoDic
objectForKey:@"CFBundleVersion"]; 获取本地版本号

NSString * URL = [NSStringstringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",APPLEID];
//请求itunes.apple应用版本信息,

NSMutableURLRequest *request = [[NSMutableURLRequestalloc]
init];
[requestsetURL:[NSURLURLWithString:URL]];
[requestsetHTTPMethod:@"POST"];
NSHTTPURLResponse *urlResponse =
nil;
NSError *error =
nil;
NSData *recervedData = [NSURLConnectionsendSynchronousRequest:request
returningResponse:&urlResponse
error:&error];

NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:recervedData
options:kNilOptionserror:&error]; //取回的数据是json格式,必须进行解析
NSArray *infoArray = [dic
objectForKey:@"results"];
if ([infoArray
count]) {
NSDictionary *releaseInfo = [infoArray
objectAtIndex:0];
NSString *lastVersion = [releaseInfo
objectForKey:@"version"]; //获取到最新版本号

//如果applestore最新版本号和本地版本不同,提醒用户更新
if (![lastVersion
isEqualToString:currentVersion]) {

//trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];

UIAlertView *alert = [[UIAlertViewalloc]
initWithTitle:@"更新"message:@"有新的版本更新,是否前往更新?"delegate:selfcancelButtonTitle:@"暂不更新"otherButtonTitles:@"更新",nil];
alert.tag =10000;
[alertshow];
}
//如果相同,提醒用户已是最新版本
else
{

UIAlertView *alert = [[UIAlertViewalloc]
initWithTitle:@"更新"message:@"此版本为最新版本"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alertshow];
}
}
}
}

//alart回调方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

//去appstore进行下载
else
if (alertView.tag ==10000){
if (buttonIndex==1) {

NSURL *url = [NSURLURLWithString:[NSStringstringWithFormat:@"https://itunes.apple.com/cn/app/wifi-xiao-guan-jia/id%@",APPLEID]];
//拼接下载地址字符串

[[UIApplicationsharedApplication]openURL:url]; //打开app所在网页
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: