App enters backstage 10 minutes active Time-B

Source: Internet
Author: User

There are 7 ways iOS allows you to run in the background for a long time:
Audio
Voip
Gps
Download News
When communicating with other accessory hardware
When using Bluetooth for communication
When using Bluetooth to share data
In addition to the above, the program may be set to run for a short period of 10 minutes
Continue running for 10 minutes when the program exits the background
Added in xxappdelegate: Uibackgroundtaskidentifier bgtask;

-(void) Applicationdidenterbackground: (uiapplication *) application{   bgtask = [application beginbackgroundtaskwithexpirationhandler:^{   //10 minutes after the execution here, there should be some cleanup work, such as disconnecting and connecting to the server      //.   .     //stopped or ending the task outright.        [application Endbackgroundtask:bgtask];     bgtask = Uibackgroundtaskinvalid;  }];  if (Bgtask = = uibackgroundtaskinvalid) {   nslog (@ "failed to start background task!");}  Start the long-running task and return immediately. Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{   //do the work   Associated with the task, preferably in chunks.      nstimeinterval timeremain = 0;      do{       [nsthread Sleepfortimeinterval:5];         if (bgtask!= uibackgroundtaskinvalid) {           timeremain = [Application BackgroundtimeremAining];              nslog (@ "Time Remaining:%f", Timeremain);         }    }while (bgtask!= uibackgroundtaskinvalid && timeremain > 0);
       //If you change to Timeremain > 5*60, the background runs for 5 minutes    //done!    //If it is not 10 minutes, you can also actively close the background task, but this needs to be executed in the main thread, otherwise error    dispatch_async (Dispatch_get_main_queue (), ^{& nbsp      IF (bgtask! = uibackgroundtaskinvalid)        {        &NBS P  //Same as code executed 10 minutes above            //...            // If you don't call Endbackgroundtask, the OS would exit your app.             [Application E   Ndbackgroundtask:bgtask];              bgtask = Uibackgroundtaskinvalid;         }    }); });} -(void) Applicationwillenterforeground: (uiapplication *) application{//If the app is opened in less than 10 minutes, end the background task if (bgtask!= Uibackgroundtaskinvalid) {   [application endbackgroundtask:bgtask];     Bgtask = Uibackgroundtask  Invalid; }}

In the background, if some code you do not want to execute, you can add the following conditions:

UIApplication *application = [UIApplication sharedApplication];if( application.applicationState == UIApplicationStateBackground) {    return;}

Some apps although we do not allow the notification, but still will pop up the message, it should be set up a timer, to a certain time to let the program run in the background, update the data from the server, and then display it.

App enters backstage 10 minutes active Time-B

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.