• 版本更新实现的思路

  • 获取自身的版本号
  • 获取AppStore的版本号
  • 自身的版本号和AppStore的比较
  • 弹窗提示所需数据的获取的方式
  • 1.获取自身的版本号
  • 2.AppStore的版本号

    WechatIMG58.jpeg

    具体实现的代码

    • 网络请求app的信息

      -(void)VersonUpdate{
      //定义的app的地址
      NSString *urld = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"你的APPID"]; //网络请求app的信息,主要是取得我说需要的Version
      NSURL *url = [NSURL URLWithString:urld];
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
      cachePolicy:NSURLRequestReloadIgnoringCacheData
      timeoutInterval:10];
      [request setHTTPMethod:@"POST"]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
      NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
      if (data) { //data是有关于App所有的信息
      NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
      if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) { [receiveStatusDic setValue:@"1" forKey:@"status"];
      [receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"]; //请求的有数据,进行版本比较
      [self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
      }else{ [receiveStatusDic setValue:@"-1" forKey:@"status"];
      }
      }else{
      [receiveStatusDic setValue:@"-1" forKey:@"status"];
      }
      }]; [task resume];
      }
    • 获取自身的版本号并与AppStore比较
    -(void)receiveData:(id)sender
    {
    //获取APP自身版本号
    NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"]; NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
    NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."]; if ((versionArray.count == 3) && (localArray.count == versionArray.count)) { if ([localArray[0] intValue] < [versionArray[0] intValue]) {
    [self updateVersion];
    }else if ([localArray[0] intValue] == [versionArray[0] intValue]){
    if ([localArray[1] intValue] < [versionArray[1] intValue]) {
    [self updateVersion];
    }else if ([localArray[1] intValue] == [versionArray[1] intValue]){
    if ([localArray[2] intValue] < [versionArray[2] intValue]) {
    [self updateVersion];
    }
    }
    }
    }
    }
    • 根据比较的结果,实现弹窗
    -(void)updateVersion{
    NSString *msg = [NSString stringWithFormat:@"更新最新版本,优惠信息提前知"];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"你的app在商店的下载地址"]];
    [[UIApplication sharedApplication]openURL:url];
    }];
    [alertController addAction:otherAction];
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil]; }

    温馨提示

    • 我这种实现的方式是根据自身的版本和线上版本,比较大小来实现的弹窗,如果比线上的版本小,就会弹窗,具体的比较看上面的代码。这样上线可以瞒天过海。
    • 当你想实现提醒更新的时候,直接的把我上面的代码,copy到AppDelegate里面即可,当然还要换上你自己的appid,具体的获取方式,上面已经写明。
    • 如果想模拟器测试效果,只要把version改的比线上的版本小就可以。
    • 我的弹窗比较“简陋”,具体的好看的效果,还需要自己去改变了。

     

最新文章

  1. Array(数组)--map方法
  2. nginx 自动忽略request中header name包含下划线参数的解决方法
  3. Java基础知识学习(七)
  4. html5用到的js
  5. Dynamics AX 2012 R2 AIF 错误 &#39;/MicrosoftDynamicsAXAif60&#39; 应用程序中的服务器错误
  6. Yii 1 数据库操作 笔记
  7. Linux递归删除文件命令
  8. 【BZOJ 3122】 [Sdoi2013]随机数生成器 (BSGS)
  9. Delphi 进阶基础技能说明
  10. 一个Jquery特效(转)
  11. mysql 发生系统错误1067的解决方法
  12. php发送get、post请求获取内容的几种方法
  13. (转)Hadoop的InputFormats和OutputFormats
  14. java程序设计习题总结
  15. Hadoop生产环境配置文件
  16. git 查看提交的信息diff
  17. vue / js使用video获取视频时长
  18. Mac OS下Android Studio的Java not found问题,androidfound
  19. 查看用户信息:w
  20. NodeList、HTMLCollection和NamedNodeMap

热门文章

  1. Mybatis --- 创建方法、全局配置
  2. 利用wireshark任意获取qq好友IP实施精准定位
  3. 深度学习框架-caffe安装-环境[Mac OSX 10.12]
  4. 阿里云centos下安装nginx、jdk、tomcat、绑定域名、解析域名
  5. JAVA 文件编译执行与虚拟机(JVM)简单介绍
  6. 利用HTML5的window.postMessage实现跨域通信
  7. Android学习记录:界面设计
  8. JOptionPane弹框常用实例
  9. 《Java程序设计》终极不改版
  10. MPLS LDP随堂笔记1