Rules for IOS running in the background
The operating state of the application is divided into the following five types:
Not running: The app has not started, or the app is running but is stopped by the system on the way.
Inactive: The current app is running in the foreground, but does not receive events (other code may be executing at the moment). Typically, a midway transition stays in this state briefly whenever the app is switching from one state to another. The only time it takes longer to stay in this state is when the user locks the screen, or the user is prompted to respond to certain events (such as phone calls, unread text messages, etc.).
Active: The current app is running in the foreground and receiving events. This is the normal state at which the app is running in the foreground
Suspended: The app is in the background and has stopped executing code. The system automatically moves the app into this state and does not notify the app before the move. When in this state, the app still resides in memory but does not execute any program code. When the system has a low memory alarm, the system will clear the Suspended state of the application to provide enough memory for the app that is running in the foreground.
Background: The app is in the background and is still executing code. The general application will only briefly stay in this state (up to 10 minutes) and then be forced into the Suspended state by the system. IOS, in order to provide a better experience in some cases, provides options that can be run in the background for a long time, as long as the conditions for these options are met, and we'll focus on ways to make the app run long in the background.
How IOS provides a background run
A list of background runs for IOS, which you can turn on in your Xcode project settings if you want. The app Store's auditors check to see if it's necessary to turn on the background run mode option in the app, and if this option is turned on, it may be rejected.
Audio, AirPlay and picture in picture
This option consists of four scenes: audio playback, recording, AirPlay and picture-in-pictures video playback.
Audio playback : When playing audio, even if the app is back in the background, as long as the audio is playing, the app can run all the time in the background.
Code implementation can be consulted: http://www.linuxidc.com/Linux/2012-08/68364.htm
recording : The app can request a microphone, and when this option is on, when the app is using the microphone, even if it's back in the background, it can be run in the background, by looking at the plist file in the installation package, the voice chat is done this way. When the app goes back to the background, the IOS system's status bar turns red and displays the name of the app that's using the microphone in the status bar, as shown in.
AirPlay: AirPlay refers to syncing audio and video on an IOS device, or on a Mac device, to play in another device. For two examples, the first one is to put the music on the iphone through Bluetooth in the car's Bluetooth audio playback, the second is to put the video on the iphone and sync it to the smart TV screen to play. This feature is typically used for multi-and multi-screen interactions.
Development documentation for AirPlay: http://nto.github.io/AirPlay.html
in-picture video playback : Pip is a new feature of the IPad version of iOS 9 that can play videos on the iOS desktop, or on top of other app's interface, so that the app that belongs to the video area can run in the background. This feature is now available only in IPad apps .
Code implementation can be consulted: http://www.cocoachina.com/ios/20150714/12558.html
Location updates
Generally used in navigation applications, when this option is turned on, the application will be back to the background, but also to get the system positioning updates, so that the application can be based on the change of positioning to make different responses.
Code implementation can be consulted: https://github.com/voyage11/Location
Voice over IP
VOIP class applications allow users to use the network instead of the phone to make calls, so this type of application needs to maintain a network connection to its associated services to receive incoming call events and other data. IOS does not do this by keeping the app active for the time being, but it also hangs the app , but it will take over its VOIP socket by the systemwhile the app is suspended, when the socket has data communication , the system wakes the app in the suspended state again and returns control of the Socket to the app, allowing it to handle incoming call events and other data normally.
Newsstand Downloads
In the development of iOS, there is a category of newspapers and magazines in particular, in the system prior to iOS 9, such applications will be unified in the system built-in "newspaper and magazine" Application, in iOS 9 removed the built-in "newspaper magazine" application, such applications can be a separate icon entry on the desktop.
The option to run after this is for newspapers and magazines to download and process the content of the magazine in the background, while the download process needs to be downloaded using the nkassetdownload in Newsstandkit. It is important to note that during the download process , the app may still be suspended , even if the app is exited , and IOS will continue to download in the Wi-Fi environment until the download is complete. Once the download is complete, if the app is just suspended , IOS wakes up the corresponding app , callbacks the corresponding event, and if the app has exited , the app is launched . The corresponding logo will be included in the startup parameters to indicate that this launch is due to the download of the newspaper and magazine content.
Code implementation can be consulted: http://www.viggiosoft.com/blog/blog/2011/10/17/ios-newsstand-tutorial/
External Accessory Communication
This option is provided for some MFi peripherals to connect to the IOS device via Bluetooth, or Lightning connectors, to wake up apps that have been suspended while the peripherals are sending messages. And once awakened, in general, the application only has a maximum of 10 seconds of execution time.
MFI Peripherals: Refers to an apple MFI-certified device, while MFi certification is an identification license for an external accessory produced by its authorized accessory manufacturer, which is an acronym for IOS made.
Uses Bluetooth LE Accessories
This option is similar to External accessory communication, except that this option does not need to limit MFi peripherals, but requires a Bluetooth LE device.
Acts as a Bluetooth LE accessory
This option refers to when an IOS device is connected as a Bluetooth peripheral, the corresponding app can run in the background, but using this mode requires the user to authorize authentication.
Background Fetch
IOS 7 Adds a new option for apps that need to update data frequently, even in the background. For example, a PM2.5 application that takes several hours to update the data, you can turn on this option to set a time interval that allows IOS to launch the app in the background for the duration of the interval and perform the acquisition of the specified data, which can take up to 30 seconds.
Code implementation can be consulted: http://objccn.io/issue-5-5/
Remote Notifications
iOS 7 adds a new option, is a silent push, it is different from the general push, the app receives this analogy sent, there will be no interface prompts, and when the app exits or hangs to receive this analogy sent, IOS will also start or wake up the corresponding app.
For example, a reading application, the user subscribed to the blog updated, then you can send a silent push, the application received this kind of push, you can first subscribe to the user's blog content to download, and then notify the user, so that users can open the application immediately to start reading.
When a silent push is received, the corresponding callback method is recalled, and this callback method can execute up to 30 seconds.
Code implementation can be consulted: http://objccn.io/issue-5-5/
Background transfer based on nsurlsession
This is a new feature of IOS 7, used to download or upload large files in the background, the steps are as follows: Create a Nsurlsession object for background transfer, add the corresponding transfer Nsurlsessiontask to this object, and start the transmission; implement in Appdelegate -application:handleeventsforbackgroundurlsession:completionhandler: Method to refresh UI and notify system transfer end.
Once the status of the background transfer changes (including normal end and failure), the app will wake up and run the callback in Appdelegate.
However, there are some limitations, and background transfers will only be done via Wi-Fi. The background download time is not the same as the previous X-minute mode of closing the app, but to save power from becoming a discrete download.
Code implementation can refer to: http://onevcat.com/2013/08/ios7-background-multitask/
Other background run black magic has been looping audio for a period of no sound
You can select "audio in the background option, AirPlay and picture in picture", and start looping through an audio that is not sound, which is used in the audio Unit callback function Kaudiounitrenderaction_ Outputissilence flag bit, but this way two major disadvantages:
1. Apple's auditors will be rejected if found;
2. The Audio Session of the application cannot be interrupted. When an app executes in the background, as long as another app uses Kaudiosessioncategory_recordandplay (such as Skype) or Kaudiosessioncategory_soloambientsound, Then the application will be interrupted immediately.
Code implementation can be consulted: Https://github.com/marcop/MMPDeepSleepPreventer
Develop System-level applications under jailbreak
General applications are Mobile-level, in the case of jailbreak, you can develop a System-level application, so that the app is not limited by the IOS general application, to achieve real background operation, but the disadvantage is that the app can only be run on jailbroken devices, and not on the app Store.
Code implementation can be consulted: http://www.cnblogs.com/doudouyoutang/p/4712331.html
IOS Background Run implementation