Running in IOS background

Source: Internet
Author: User

After upgrading to 4.0, IOS supports multiple tasks. This feature is described in detail below.

1. Check whether the device supports multiple tasks.

For performance considerations, not all IOS devices support multitasking after being upgraded to ios4, such as the iPhone
3G. If your application has no multi-task features, you should judge and handle them to ensure the robustness of the application. You can call the multitaskingsupported attribute of the uidevice object to determine whether the current device supports multiple tasks.

If (! [Uidevice currentdevice]. multitaskingsupported ){

// Processing when multiple tasks are not supported

}

2. Basic multitasking features

Generally, when you press the Home key, the current application will be in the background. After the application is in the background running state for a short period of time, it will enter the suspend state, at this time, the application will not execute any code. If the system has insufficient memory resources when running other applications, the suspended application may even be exited by the system, releasing the memory for active applications. Only when the user runs the application again will the application be awakened from the pending state and the code can continue to be executed. This is the basic multitasking feature of ios4. This feature is generally supported by applications by default, that is, your applications can support basic multitasking features without any modifications.

Since it is a multi-task, you should perform some processing when the application enters the background, such as pausing some interface timed refresh or network requests. At the same time, you may perform some recovery operations when the program enters the foreground. There are two messages in your application's application delegate to process these messages:

-(Void) applicationdidenterbackgRound :( uiapplication *) Application {
// Processing to be performed when entering the background
}

-(Void) applicationwillenterforeGround :( uiapplication *) Application {
// The process to be performed when entering the front-end
}

Of course, you may not all handle all the tasks in application delegate. If you want to process it in other objects, You need to register the system notification. The two notifications are uiapplicationdidenterbac.KgroundnotificationAnd uiapplicationwillenterfoRegroundnotification.

You may need more multitasking features, such as playing music in the background or performing GPS tracking in the background. This will be the content described below.

3. play music in the background

Generally, when an application enters the background, any sound stops. This may not be what we want. To make your application support Background playback, you must first modify the info of the application. plist file, you need to go to info. add the uibackgroundmodes field to the plist file. The value of this field is an array of all background modes supported by the application. Current quantity
The Group can contain three string constants: "audio", "location", and "VoIP". To support playing background music, you must include "audio ", the remaining two will be discussed later.

At the same time, you should set the application's audio sesstion. This is required. If you do not set audio sesstion, the app may stop playing when audio sesstion is disabled in the background. You can set it as follows:

[[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 the "location" string to the array corresponding to the uibackgroundmodes field in the info. plist file.

5. backend VoIP support

As a VoIP application requires a persistent connection to the server, in order to make such applications work normally, the background VoIP Support feature is assumed in IOS. To support this feature, you need to add the "VoIP" string to the array corresponding to the uibackgroundmodes field in the info. plist file.

In addition, you still need to configure your network connection to support backend connections. There are several network connection libraries provided by IOs, which are described as follows:

If you are using nsstream, such as nsinputstream or nsoutputstream, you need to call setproperty: forkey: Set the key to nsstreamnetworkservicetySet PE value 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 must call cfreadstreamsetproperty or cfwritestreamsetproperty.Set kcfstreamnetworkservicetSet the ype attribute to kcfstreamnetworkservicet.Ypevoip.


1. Run the logic code:

-(Void) execbackrgoundmethod

{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationwillterminate: when the user quits.
*/
Uiapplication * APP = [uiapplication sharedapplication];
Bgtask = [App beginbackgroundtaskwithexpirationhandler: ^ {
[App endbackgroundtask: bgtask];
Bgtask = uibackgroundtaskinvalid;
}];
// Start the long-running task and return immediately.
Dispatch_async (dispatch_get_global_queue (dispatch_queue_priority_default,
0), ^ {// write the code you want to run in the back
[App endbackgroundtask: bgtask];
Bgtask = uibackgroundtaskinvalid;
});
}


2. uilocalnotification is displayed in the background:

-(Void) schedulealarmfordate :( nsdate *) thedate
{
// How long will thedate be delayed?
Uiapplication * APP = [uiapplication sharedapplication];
Nsarray * oldnotifications = [App scheduledlocalications ications];
// Clear out the old notification before scheduling a new one.
If ([oldnotifications count]> 0)
[App cancelalllocalconfigurications];
// Create a new notification.
Uilocalnotification * Alarm = [[uilocalnotification alloc] init] autorelease];
If (alarm)
{
Alarm. firedate = thedate;
Alarm. timezone = [nstimezone defaulttimezone];
Alarm. repeatinterval = 0;
Alarm. soundname = @ "alarmsound. caf ";
Alarm. alertbody = @ "Time to wake up! ";
[App schedulelocalnotification: Alarm];
}
}

For more details, please click the link below:

Https://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Http://mobile.tutsplus.com/tutorials/iphone/ios-multitasking-local-notifications/

Related Article

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.