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