In the previous article, "app states and multitasking iOS app status and multitasking," The iOS program has a front-end background. After the iOS program has entered the background, the program will not execute the code, if there is no need to perform any process, you can request about 600s of time, if the program is not completed during this time will be killed by the system. Appears: has active assertions beyond permitted time. More IOS Background content reference: <IOS7 Background Fetch background Application refresh > <ios 74 kinds of background mechanism >
After entering the backstage, Nstimer and so on will not work. There are many places on the web that refer to the way in the background:
-(void) Applicationdidenterbackground: (uiapplication *) application
Add the following code to keep the timer functioning in the background:
FoldingC + + Codecopy content to clipboard
- uiapplication* app = [UIApplication sharedapplication];
- __block Uibackgroundtaskidentifier Bgtask;
- Bgtask = [App beginbackgroundtaskwithexpirationhandler:^{
- Dispatch_async (Dispatch_get_main_queue (), ^{
- if (Bgtask! = uibackgroundtaskinvalid)
- {
- Bgtask = Uibackgroundtaskinvalid;
- }
- });
- }];
- Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
- Dispatch_async (Dispatch_get_main_queue (), ^{
- if (Bgtask! = uibackgroundtaskinvalid)
- {
- Bgtask = Uibackgroundtaskinvalid;
- }
- });
- });
In the cool bar test process, using the above method program background run about 600s after the system kill. In fact, the effect of the above code is to request extra run time, which will be killed when the background runs out. I do not know the first to write in this way the students have tried their own. Cool bar in the test found that the investigation into the background of the State do not use the simulator, nor in the debug state. It is best to write the file log, the real machine without debugging test, that is really into the background of the state.
About the iOS program background long-running problem, cool bar This two days of understanding the result is: In addition to iOS support audio, location positioning, VoIP and other few features, others are difficult to do. Where you need to configure the Plist file to add when using audio, location locator, and other backend servicesRequired Background ModesKey, and addAPP registers for location updates,App plays audio or streams audio/video using AirPlayTwo key values.
The APP registers for location updates is primarily targeted at the positioning service class Cllocationmanager, with a new location called-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (Nsarray *) Locations method, within the method, can do the record location data, etc., which can be completed in the background.
APP plays audio or streams audio/video using airplay is primarily for audio playback recording class Avaudioplayer,avaudiorecorder, etc. before using to get the session first
Avaudiosession *session = [Avaudiosession sharedinstance];
[Session Setactive:yes Error:nil];
[Session Setcategory:avaudiosessioncategoryplayback Error:nil];
Reprint please indicate from Cool bar, this article address: http://www.tekuba.net/program/318/
Reference reading:
iOS screen rotation control detailed iOS local and remote push notifications in detail
Communicating with and other app communication iOS app launch process lifecycle detailed
iOS development UIScreen and Uiwindow,uiview app submission to AppStore process
An analysis of iOS background operation