Implementation code for iOS long time background run
1, Check whether the device supports multi-tasking
Apple for performance reasons, not all iOS devices are supported for multitasking after upgrading to iOS4, such as the iphone 3G. If your app has problems with no multitasking features, you should judge and handle it in order to maintain the robustness of your app. You can get the current device to support multiple tasks by calling the Multitaskingsupported property of the Uidevice object.
[CPP]View Plaincopy
- if (![ uidevicecurrentdevice].multitaskingsupported)
- {
- //Do not support multi-task handling
- }
Typically, when the user presses the home key, the current app is entered in the background, and after a short period of time the app is running in the background, it enters the suspend (suspend) state, where the app no longer executes any code. If the system runs out of memory resources while running other applications, the suspended application may even be exited by the system, freeing up memory for use by the active application. The app will wake up from the suspended state only when the user runs the app again, and the code can continue to execute. This is the basic multitasking feature of iOS4, which is supported by the general application by default, which means that your application does not require any modification to support the basic multitasking feature. 2, basic multi-task characteristics
Since it's multitasking, you should do some processing when the app is in the background, such as pausing some of the interface's timed refreshes or network requests. At the same time, you may perform some recovery operations when the program enters the foreground. There are 2 messages in your app's application delegate for handling these messages:
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- -(void) Applicationdidenterbackground: (uiapplication*) Application
- {
- //processing to enter the background
- }
- -(void) Applicationwillenterforeground: (uiapplication*) Application
- {
- //The processing to be carried out when entering the foreground
- }
Maybe you need more multitasking features, like playing music in the background or a GPS track in the background. This will be the content described below. Of course you may not all be application delegate to deal with everything. If you're dealing with other objects, you'll need to register for system notifications. The two announcements were uiapplicationdidenterbackgroundnotification and uiapplicationwillenterforegroundnotification.
3 . Declare the background tasks you need
Adds a Uibackgroundmodes key value in Info.plist that contains one or more string values, including:
Audio: Provides sound playback functionality in the background, including audio streaming and sound when playing video
Location: In the background you can keep the user's position information
VoIP: Use VoIP features in the background
Each of the preceding value lets the system know that your application should be awakened at the appropriate time. For example, an application that starts playing music and then moves to the background still requires execution time to fill the audio output buffer. Adding the audio key is used to tell the system frame that it needs to resume playback and can callback the application at the appropriate time interval, and if the application does not include this, any audio playback will stop running after moving to the background.
In addition to the method of adding key values, iOS provides two ways to make your application work in the background:
The task completion-application can request additional time from the system to complete a given task
Local notifications-application can schedule time to perform local notifications delivery
4. play Music in the background
Generally, when you enter the background, any sound will stop. This may not be what we want. To enable your app to support background playback, first modify the app's Info.plist file, you need to add the Uibackgroundmodes field in the Info.plist file, the value of which is all background mode supported by the app, is a numeric type. Currently this array can contain "audio", "Location" and "VoIP" three string constants, if you want to support background music playback, you need to include "audio", the remaining two will be mentioned later.
Also, you should set the audio sesstion for your application. This is required, and if the audio sesstion is not set, the app may go into the background when audio sesstion is inactivated and stops playing. This is usually the way to set it up:
[CPP]View Plaincopy
- [[Avaudiosessionsharedinstance]setcategory:avaudiosessioncategoryplaybackerror:nil];
5. Background GPS Tracking
Similar to playing music in the background, to support background GPS tracking, you need to add a "location" string to the array in the Info.plist file that corresponds to the Uibackgroundmodes field.
6. Background VoIP Support
Because VoIP applications require a long connection to the server, in order for such applications to work properly, iOS adds the background VoIP support feature. To support this feature, you need to add a "VoIP" string to the array that corresponds to the Uibackgroundmodes field in the Info.plist file.
In addition, you still need to configure your network connection in order to support the background connection. There are several network connection libraries available in iOS, as described in the following one by one:
A, if you are using Nsstream, such as Nsinputstream or nsoutputstream, you need to call Setproperty:forkey: Set the value of key nsstreamnetworkservicetype to
Nsstreamnetworkservicetypevoip;
B, If you use nsurlrequest, you need to call Setnetworkservicetype: Set the network type to Nsurlnetworkservicetypevoip;
C, If you use Cfstream, such as Cfreadstreamref or CFWRITESTREAMREF, You need to call Cfreadstreamsetproperty or Cfwritestreamsetproperty to
The Kcfstreamnetworkservicetype property is set to Kcfstreamnetworkservicetypevoip.
iOS background operating mechanism