欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios实现app强制更新功能

程序员文章站 2024-02-13 09:26:17
最近因项目需求,需要用到强制更新功能,网上搜了一下,挺多的,但是把网上的代码拷贝以后,发现一个bug,就是进app,弹出框显示,点击现在升级,跳转到appstore下载里面...

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

效果图:

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];
}

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