网站建设资讯

NEWS

网站建设资讯

ios实现app强制更新功能-创新互联

最近因项目需求,需要用到强制更新功能,网上搜了一下,挺多的,但是把网上的代码拷贝以后,发现一个bug,就是进app,弹出框显示,点击现在升级,跳转到AppStore下载里面,但是我不下载,又切回到app里面,发现弹出框就不跳了,其实也简单,就是appdelegate里面有个代理方法,就是当app从后台切到前台走的方法,将强制更新方法在这里面在调用一下就行了~~~话不多说,上代码!!!用的话直接粘贴复制~

创新互联于2013年成立,是专业互联网技术服务公司,拥有项目成都网站设计、做网站、成都外贸网站建设公司网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元礼县做网站,已为上家服务,为礼县各地企业和个人服务,联系电话:028-86922220

效果图:

ios实现app强制更新功能

在appdelegate里面写下面代码

//提示版本更新
 [self VersonUpdate];
#pragma mark ------提示用户版本更新------

-(void)VersonUpdate{

  //定义app地址
  NSString *urld = [NSString  stringWithFormat:@"http://itunes.apple.com/lookup?id=%d",1178114725];

  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) {

    NSLog(@"%@",response);

    NSMutableDictionary *receiveStatusDic = [[NSMutableDictionary alloc]init];

    if (data) {

      NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
      if ([[receiveDic valueForKey:@"resultCount"] intValue] > 0) {

        [receiveStatusDic setObject:@"1" forKey:@"status"];

        [receiveStatusDic setObject:[[[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];

}
-(void)receiveData:(id)sender
{
  //获取APP自身版本号
  NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];

  NSArray *localArray = [localVersion componentsSeparatedByString:@"."];//1.0
  NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];//3 2.1.1


//  if ((versionArray.count == 2) && (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:@"https://itunes.apple.com/cn/app/m-help/id1178114725?mt=8"]];
    [[UIApplication sharedApplication]openURL:url];
  }];
  [alertController addAction:otherAction];
  [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];

}
//当app从后台切到前台时调用的方法
- (void)applicationDidBecomeActive:(UIApplication * )application
{
  [self VersonUpdate];
}

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


标题名称:ios实现app强制更新功能-创新互联
分享网址:http://njwzjz.com/article/hdhge.html