ios实现应用强制更新功能

  

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

  

效果图:

  

 ios实现应用强制更新功能

  

在appdelegate里面写下面代码

     //提示版本更新   (自我VersonUpdate);   # pragma马克- - - - - -提示用户版本更新- - - - - -      - (void) VersonUpdate {//定义应用地址   NSString * urld=[NSString stringWithFormat: @ " http://itunes.apple.com/lookup& # 63; id=% d ", 1178114725);      NSURL * url=[NSURL URLWithString urld):;      NSMutableURLRequest *请求=[NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 10];      (请求setHTTPMethod: @“POST”);      NSURLSession *会话=[NSURLSession sharedSession];      NSURLSessionDataTask *任务=[会话dataTaskWithURL: url completionHandler: ^ (NSData * _Nullable数据,NSURLResponse * _Nullable响应,NSError * _Nullable错误){      NSLog(@ % @,响应);      称为unaryoperations * receiveStatusDic=[(称为unaryoperations alloc) init);      如果(数据){      NSDictionary * receiveDic=[NSJSONSerialization JSONObjectWithData:数据选择:NSJSONReadingMutableLeaves错误:nil);   如果([[receiveDic valueForKey: @ " resultCount "] intValue]比;0){      [receiveStatusDic setObject: @“1”forKey: @“地位”);      [receiveStatusDic setObject: [[[receiveDic valueForKey: @ "结果"],objectAtIndex: 0] valueForKey: @“版本”)forKey: @“版本”);      【自我performSelectorOnMainThread: @ selector (receiveData:) withObject: receiveStatusDic waitUntilDone:没有);         其他}{      [receiveStatusDic setValue: @“1”forKey: @“地位”);         }   其他}{         [receiveStatusDic setValue: @“1”forKey: @“地位”);   }         });      (任务的简历);      }   - (void) receiveData:发送者(id)   {//获取应用自身版本号   NSString * localVersion=[[[NSBundle mainBundle] infoDictionary] objectForKey: @“CFBundleShortVersionString”);      NSArray * localArray=[localVersion componentsSeparatedByString: @ " . ");//1.0   NSArray * versionArray=(发送者[@“版本”]componentsSeparatedByString: @“。”);//3 2.1.1//如果(versionArray。数==2),,(localArray。数==versionArray.count)) {      如果([localArray [0] intValue] & lt;[versionArray [0] intValue]) {      (自我updateVersion);      }else if ([localArray [0] intValue]==[versionArray [0] intValue)) {   如果([localArray [1] intValue] & lt;[versionArray [1] intValue]) {   (自我updateVersion);      }else if ([localArray [1] intValue]==[versionArray [1] intValue]) {   如果([localArray [2] intValue] & lt;[versionArray [2] intValue]) {      (自我updateVersion);      }   }   }//}      }      - (void) updateVersion {      NSString *味精=[NSString stringWithFormat: @”版本过低,需要升级到最新版本”);   UIAlertController * alertController=[UIAlertController alertControllerWithTitle: @”升级提示”信息:味精preferredStyle: UIAlertControllerStyleAlert);   UIAlertAction * otherAction=[UIAlertAction actionWithTitle: @”现在升级”风格:UIAlertActionStyleDestructive处理程序:^ (UIAlertAction *行动){      NSURL * url=[NSURL URLWithString:(NSString stringWithFormat: @ " https://itunes.apple.com/cn/app/m-help/id1178114725& # 63;太=8 "]];   [[UIApplication sharedApplication] openURL: url];   });   [alertController addAction otherAction):;   [self.window。rootViewController presentViewController: alertController动画:是的完成:nil);      }//当应用程序从后台切到前台时调用的方法   - (void) applicationDidBecomeActive: UIApplication(*)应用程序   {   (自我VersonUpdate);   }      之前      

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

ios实现应用强制更新功能