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

iOS检查更新的方法

2015-12-10 13:30 381 查看
iOS检查更新的方法

一些应用要检查更新,基本思路是获取当前版本号,然后解析url里面的版本号,并讲2者进行对比判断。代码如下:

- (IBAction)button:(id)sender {

NSString * string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@”http://itunes.apple.com/lookup?id=XXX“] encoding:NSUTF8StringEncoding error:nil];

if (string != nil &&[string length]>0 &&[string rangeOfString:@”Version”].length == 7) {

[self Postpath:string];

}

}

// 解析url,获取当前在服务器上面的版本号

- (void)Postpath:(NSString *)appInfo{

//获取本地的版本号

NSString * version = [[[NSBundle mainBundle]infoDictionary]objectForKey:@”CFBundleShortVersionString”];

//截取出url上面的版本号

NSString * appInfo1 = [appInfo substringFromIndex:[appInfo rangeOfString:@”\”version\”:”].location+10];

appInfo1 = [[[appInfo1 substringToIndex:[appInfo rangeOfString:@”,”].location]stringByReplacingOccurrencesOfString:@”\”” withString:@”“]componentsSeparatedByString:@”,”][0];

if (![appInfo1 isEqualToString:version]) {

UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@”提示” message:@”发现新版本,需要升级么?” delegate:self cancelButtonTitle:@”确定” otherButtonTitles:@”取消”, nil];

alert.tag = 999;

[alert show];

}else{

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@”提示” message:@”已经是最新版本了” cancelButtonTitle:@”确定” otherButtonTitles:nil];

[alert show];

}

}

//alertView的代理方法

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

{

if (alertView.tag==999){

if(buttonIndex==0) {

[self updataApp];

}

else{

[alertView dismissWithClickedButtonIndex:1 animated:YES];

};

}

}

//更新app

-(void)updataApp{

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”https://itunes.apple.com/cn/app/XXXX“]];

}

在需要的时候把地址更换一下就可以了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 版本更新