首页 > 移动平台 > 详细

iOS 版本自动更新

时间:2015-08-12 17:05:12      阅读:203      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!