码迷,mamicode.com
首页 > 移动开发 > 详细

iOS 版本自动更新

时间:2015-08-12 17:07:32      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

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]];
    }
}


iOS 版本自动更新

标签:

原文地址:http://my.oschina.net/u/2418604/blog/491277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!