AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self editionUpdate];
}
//版本自动更新
-(void)editionUpdate{
NSString * url=@"192.168.......";//获取后台的接口
// NSString * url=[NSString stringWithFormat:@" //获取app store的接口
//使用同步请求进行解析数据
NSMutableURLRequest *request=[[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:url]];
NSData *returnData=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *jsonData=[NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil ];
NSString *updateVersion=[jsonData objectForKey:@"version"];//版本号
_updateUrl=[jsonData objectForKey:@"trackViewUrl"];//下载地址
//获取应用当前版本
NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
//转为double类型
double currentVersion=[version doubleValue];
double updaVersion=[updateVersion doubleValue];
if (currentVersion<updaVersion) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"提示" message:@"有新版本可更新" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
[alert show];
}
}
//alertView delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==1) {
// 通过获取到的url打开应用在appstore,并跳转到应用下载页面
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:_updateUrl]];
}
}
原文:http://my.oschina.net/u/2418604/blog/491277