iOS一句代码更新app

一句代码更新app

获取线上app版本信息

     链接:http://itunes.apple.com/cn/lookup?id=%@ ;id表示app的apppid。

设备上app版本信息

   
 //获取本地软件的版本号
 NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"];

跳转appstore更新程序

   https://itunes.apple.com/cn/app/appName/id%@?mt=8&ls=1 

   appName: app的名字

   id: app的apppid

直接上代码吧!

+ (void)appUpdateWithAPPID:(NSString *)appid
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
   {
       NSString *newVersion;
       //添加应用的ID
       NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",appid]];
       //通过url获取数据
       NSString *jsonResponseString =  [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
       NSData*jsonData = [jsonResponseString dataUsingEncoding:NSUTF8StringEncoding];
       //解析json数据为数据字典
       NSDictionary *loginAuthenticationResponse;
       if (jsonData) {
           loginAuthenticationResponse =[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
       }else{
           return;
       }
       //从数据字典中检出版本号数据
       NSArray *configData = [loginAuthenticationResponse valueForKey:@"results"];
       for(id config in configData)
       {
           newVersion = [config valueForKey:@"version"];
       }
       
       //获取本地软件的版本号
       NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleShortVersionString"];
       NSString *msg = [NSString stringWithFormat:@"您当前的版本是V%@,发现新版本V%@,是否下载新版本?",localVersion,newVersion];
       //对比发现的新版本和本地的版本
       if ([newVersion floatValue] > [localVersion floatValue])
       {
           dispatch_async(dispatch_get_main_queue(), ^{
               
               UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"升级提示!" message:msg preferredStyle:UIAlertControllerStyleAlert] ;
              
               UIAlertAction *ActionTrue = [UIAlertAction actionWithTitle:@"现在升级" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    NSString *strUrl = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/NSWJBtVerify/id%@?mt=8&ls=1",appid];
#ifdef IOS9_Update
                  NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString: @"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "];
                  strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:characterSet];
#else
                  strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
#endif
                   
#ifdef IOS10_Update
                   [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strUrl] options:@{UIApplicationOpenURLOptionUniversalLinksOnly : @NO} completionHandler:^(BOOL success) {
                           if (!success) {
                               UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"跳转失败情重新尝试!" preferredStyle:UIAlertControllerStyleAlert];
                               UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleCancel handler:nil];
                               [aler addAction:cancelAction];
                               dispatch_async(dispatch_get_main_queue(), ^{
                                  [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:aler animated:YES completion:nil];
                               });
                           }
                       }];
#else
                   
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
                   
                       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strUrl]];
#pragma clang diagnostic pop
                   
#endif
               }];
               UIAlertAction *ActionFalse = [UIAlertAction actionWithTitle:@"下次" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
               }];
               [alertView addAction:ActionFalse];
               [alertView addAction:ActionTrue];
               dispatch_async(dispatch_get_main_queue(), ^{
                   [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertView animated:YES completion:nil];
               });
           });
       }
       else{
           
       }
   });
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值