IOS iPhone details are executed in the IOS background

Source: Internet
Author: User

The following are some of your friendly support:

Made a product, need popularity support a bit, Android and iPhone 91 market search # super junior mission #, or directly to the page to download the http://m.ixingji.com/m.html? P = x16

Official documents: http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Good Chinese articles

Http://www.devdiv.com/thread-47004-1-1.html

Http://www.cnblogs.com/haipingwu/archive/2011/03/18/1987962.html

More background time for ios4 requests

Http://blog.csdn.net/zhangao0086/article/details/6739266


Execution in the IOS background is the content described in this article. Most applications will be paused shortly after they enter the background status. In this state, the application does not execute any code and may delete it from the memory at any time. Applications provide specific services. Users can request the backend execution time to provide these services.

Determine whether multithreading is supported

UIDevice* device = [UIDevice currentDevice];  BOOL backgroundSupported = NO;  if ([device respondsToSelector:@selector(isMultitaskingSupported)])  backgroundSupported = device.multitaskingSupported; 


Declare the background task you need

Add the uibackgroundmodes key value to info. plist. It contains one or more string values, including

Audio: provides sound playing functions in the background, including audio streams and sounds during video playback.

Location: the user's location information can be maintained in the background.

VoIP: Use the VoIP function in the background

Each of the preceding values let the system know that your application should be awakened when appropriate. For example, an application that starts playing music and then moves to the background still needs to execute time to fill the audio output buffer. The audio key is added to tell the system framework that the audio needs to be played continuously and the application can be called back at an appropriate interval. If this option is not included in the application, any audio playback stops running after it is moved to the background.

In addition to the key value adding method, IOS also provides two ways for applications to work in the background:

Task completion-the application can apply for additional time from the system to complete the specified task.

Local communications-applications can schedule time for local communications transfers

Implement long background tasks

Applications can request to run in the background to implement special services. These applications do not run continuously, but are awakened by the system framework at the right time to implement these services.

1. tracking user location: omitted

2. play audio in the background:

1. // avaudiosession * session = [avaudiosession sharedinstance]; [session setactive: Yes error: Nil]; [session setcategory: avaudiosessioncategoryplayback error: Nil]; 2. allows the background to Handle multimedia events [[uiapplication sharedapplication] beginreceivingremotecontrolevents]; remote-control events originate as commands issued by headsets and external accessories that are intended to control multimedia presented by an applica Tion. To stop the alert tion of remote-control events, you must call endtransferingremotecontrolevents.3. when the system enters the background, the program can run for a period of time. Use this method to get a certain amount of time and process some things after the program enters the background. -(Uibackgroundtaskidentifier) beginbackgroundtaskwithexpirationhandler :( void (^) (void) handlerthis method lets your application continue to run for a period of time after it transitions to the background. your application cocould call this method to ensure that had enough time to transfer an important file to a remote server or at least attempt to make the transfer and note any errors. you shoshould not use this method simply to keep your application running after it moves to the background.

3. Implement VoIP applications:


Complete a limited-length task in the background

Official Document http://developer.apple.com/library/iOS/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

At any time before termination, the application calls beginbackgroundtaskwithexpirationhandler: The method allows the system to provide additional time to complete some tasks that need to be executed for a long time in the backend. (The backgroundtimeremaining attribute of the uiapplication contains the total runtime of the program)

You can use task completion to ensure that programs that are important but require long running will not be suddenly closed because the user enters the background. For example, you can use this function to save user information to a disk or download an important file from the network. There are two ways to initialize such a task:

1. Use beginbackgroundtaskwithexpirationhandler: And endbackgroundtask: To package important tasks that run for a long time. This protects these tasks from being interrupted when the program suddenly enters the background.

2. When your application delegates applicationdidenterbackground: The method is called and starts the task again.

The two methods in must correspond One to One. endbackgroundtask: The method tells the system that the task has been completed and the program can be terminated at this time. Because the application only has limited time to complete background tasks, you must call this method before timeout or the system is about to terminate the program. To avoid termination, you can also provide an expiration handler and endbackgroundtask at the beginning of a task: method. (You can view the backgroundtimeremaining attribute to determine the remaining time ).

A program can provide multiple tasks at the same time. Whenever you start a task, beginbackgroundtaskwithexpirationhandler: The method returns a unique handler to identify the task. You must pass the same handler in the endbackgroundtask: Method to terminate the task.

Listing 4-2 Starting a background task at quit time  - (void)applicationDidEnterBackground:(UIApplication *)application  {  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), ^{  // Do the work associated with the task.  [app endBackgroundTask:bgTask];  bgTask = UIBackgroundTaskInvalid;  });  } 

In the preceding example, the bgtask variable is a member variable of a class and stores a pointer to the background task id.

In expriation handler, you can add the Code required to close the task. However, the added Code cannot be executed for too long. When the expriation handler is called, the program is very close to being closed, therefore, only a very short time is required to clear the status information and terminate the task.

Arrange local notification transmission

The uilocalnotification class provides a way to pass local communications. Unlike push notifications, remote servers must be configured. local communications are arranged in the program and executed on the current device. This capability can be used if the following conditions are met:

1. A time-based program allows the program to post an alert at a specific time in the future, such as an alarm clock.

2. A program running in the background, post a local notification to attract user attention

To arrange the transmission of local notification, you need to create a uilocalnotification instance, set it, and use the uiapplication class method to arrange it. The local notification object contains the type (sound, alert, or badge) to be passed and the time to be presented ). The uiapplication method provides options to determine whether to transfer immediately or at the specified time.

Listing 4-3 Scheduling an alarm notification  - (void)scheduleAlarmForDate:(NSDate*)theDate  {  UIApplication* app = [UIApplication sharedApplication];  NSArray* oldNotifications = [app scheduledLocalNotifications];  // Clear out the old notification before scheduling a new one.  if ([oldNotifications count] > 0)  [app cancelAllLocalNotifications];  // 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];  }  } 

(It can contain up to 128 local communications active at any given time, any of which can be configured to repeat at a specified interval .) if the program is already in the foreground when the notification is called, the application: didreceivelocalnotification: method will be replaced.

Summary: The detailed description of the content executed on the IOS background is complete. I hope this article will help you!


 
 

    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.