Implementation of various background scenarios for iOS

Source: Internet
Author: User

IOS has supported multitasking after upgrading to 4.0. This feature is described in more detail below.

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.

if (![ Uidevice currentdevice].multitaskingsupported) {

What to do if multiple tasks are not supported

}

2, basic multi-task characteristics

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 app does not require any modifications to support basic multi-tasking features.

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:

-(void) Applicationdidenterbackground: (uiapplication *) Application {
What to do when entering the background
}

-(void) Applicationwillenterforeground: (uiapplication *) Application {
Handling when entering the foreground
}

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, which are uiapplicationdidenterbackgroundnotification and Uiapplicationwillenterforegroundnotification.

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.

3. 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:

[[Avaudiosession sharedinstance] Setcategory:avaudiosessioncategoryplayback Error:nil];

4. 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.

5. Background VoIP Support

Since VoIP applications require a long connection to the server, in order for such applications to work properly,IOS should have background VoIP support features. 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:

If you are using Nsstream, such as Nsinputstream or nsoutputstream, you need to call Setproperty:forkey: Set the value of key to Nsstreamnetworkservicetype to
Nsstreamnetworkservicetypevoip

If you use nsurlrequest, you need to call Setnetworkservicetype: Set the network type to Nsurlnetworkservicetypevoip

If you use Cfstream, such as Cfreadstreamref or cfwritestreamref, you need to call Cfreadstreamsetproperty or Cfwritestreamsetproperty to The Kcfstreamnetworkservicetype property is set to Kcfstreamnetworkservicetypevoip.

Implementation of various background scenarios for iOS

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.