Home>

Recently due to project needs,Need to use the forced update function,Searched online,Quite a lot, but after copying the online code,Found a bug, just enter the app, a pop-up box displays,Click Upgrade Now,Jump to the appstore download,But I do n’t download,Switch back to the app and find that the pop-up box does not jump.It's actually simple,There is a proxy method in appdelegate,Is when the app is switched from the background to the foreground,Just call the forced update method here ~~~ Not much to say,On the code! !! !! If you use it, paste it and copy it ~

Effect picture:

Write the following code in appdelegate

 //Prompt version update
 [self versonupdate];
#pragma mark ------ Prompt user for version update ------
-(void) versonupdate {
  //define app address
  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
{
  //Get the app version number
  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:@"version is too low,Need to upgrade to the latest version "];
  uialertcontroller * alertcontroller=[uialertcontroller alertcontrollerwithtitle:@"Upgrade Alert" message:msg preferredstyle:uialertcontrollerstylealert];
  uialertaction * otheraction=[uialertaction actionwithtitle:@"Upgrade now" 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];
}
//Method called when the app is switched from the background to the foreground
-(void) applicationdidbecomeactive:(uiapplication *) application
{
  [self versonupdate];
}
ios
  • Previous Simple and practical example code for Ajax
  • Next C # operation ftp class complete example