A simple solution to the iOS background operating mechanism

Source: Internet
Author: User

1.iOS is actually a pseudo-backstageWhy is iOS a pseudo-backend? The first thing you need to know about the background process in iOS is what's going on. The so-called "background dwell" in iOS does not refer to the program in process, but to the recently used program. When we are a process into the background, it can be imagined as a command+tab keyboard. In general, when the app back to the background will have a 5s of time to perform the buffer, some programs can be required to reach 10 minutes (this is required by the developer to decide), so after the home button, the original program will fall back to the background, up to 10 minutes will still be terminated, This, of course, has to do with Apple's backend management mechanism.   2.iOS Multi-taskingSince the advent of the operating system there is no multitasking, just like multithreading, is nothing more than the CPU in a very short period of time between multiple processes. The single task is the current process exclusive resource. Simply put, the type of app after iOS4 can be divided into three types: 1. Save the scene. That is: Press the home button to kill the process directly within 10 seconds and free up memory. In general, all programs have a 5-second buffer time after they enter the background, and some programs can be extended to 10 minutes (which is, of course, determined and declared by the developer when designing and submitting the program); So, after you press the home key, the original program will fall back to the background, if it has an extra background to perform the job, More than 10 minutes will still be aborted by iOS. 2. "Multitasking" supported by iOS. That is: Press the home button into the multi-tasking state, reserved in memory, but only the system allows the action: such as GPS, such as streaming music and so on. 3. Real desktop-level multitasking. That is: Only Safari/mail is the real multi-tasking, most of the Apple direct descendant are not. This level of app in the background is no restriction action. (for the Unrestricted action of the program, one will be the user unaware of the situation of power consumption, and the second is the security above the problem). 3. Multi-tasking of equipmentFor performance reasons, Apple has not previously supported multiple tasks for all devices. Usually, when the user presses the home button, the current application will enter the background, the application in the background will be suspend (hang) state, at this time no longer to execute any code, if the system in the run of other programs in memory warning will be prioritized first suspend program first exit the system, Only when the user and the app is launched again from the suspended state into the state of wake, this is the basic multi-tasking special new. So since multitasking should be done when the app is in the background, such as pausing a timed refresh of the interface or a network request, or needing to perform some recovery operations when the program enters the foreground, there are two ways to process these messages in the application's appdelegate: (void) Applicationdidenterbackground: (uiapplication*) application   {      handling when entering the background   }  -  (void) Applicationwillenterforeground: (uiapplication*) Application {     //processing to be processed when entering the foreground   }  if the background messages are processed in other objects, Need to use two notifications to the system uiapplicationdidenterbackgroundnotification and uiapplicationwillenterforegroundnotification. So in development we often need to do something in the background, such as GPs, playing music and so on. So this time we need to manually declare the task in the background: Add the Uibackgroundmodes key value in Info.plist, which contains one or more string values, including: Audio: Provides sound playback functionality in the background, including audio streaming and sound location when playing video: in the background can Keep the user's location VoIP: Every value in front of the VoIP feature in the background lets the system know that our application should be awakened at the right time, such as when an application needs to play music in the background, add the audio key to tell the system frame, and need to continue playing the audio, The application can be recalled at the appropriate time interval, and if the application does not include this, any audio playback will stop running after moving to the background. AfterVoIP 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.

A simple solution to the iOS background operating mechanism

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.