By Neha Gupta

 

Created

4 May 2012

On iOS, an application in the foreground moves to background when user presses the home button, or presses sleep/wake button, or the system launches another application. Most applications get suspended on transition to background. Applications that have requested for background execution (such as playing music, location updates, file upload/download) may continue to run for a while longer. In order to improve the device battery life and user’s experience with the foreground application, iOS limits what an application can do in the background.
 
Default Behavior of AIR application on iOS
By default, an AIR application on iOS gets suspended on entering background, primarily to preserve application’s in-memory state. Thus allowing the application to be quickly re-activated when it is brought to the foreground. When a low-memory condition occurs, the system may purge suspended applications without notice to make more space for the foreground application.
 
Support for audio playback in background
AIR on iOS provides support for playing audio in the background. Audio playback in the background is required for designing music player sort of application which plays music in both foreground and background. If an application developer wishes to play audio in the background, “UIBackgroundModes” tag with value “audio” needs to be set in the application descriptor.

<iPhone>
    <InfoAdditions
        <![CDATA[
             <key>UIBackgroundModes<\key>
             <array>
                 <string>audio</string>
            </array>
    ]]>
    </InfoAdditions>
</iPhone>

If UIBackgroundModes is set to audio  then audio from an AIR application would continue playing when it goes to background. In order to conserve battery life, all screen updates would be disabled and application would be throttled down to 4 fps in the background.
 
Support for location updates in background
AIR on iOS provides support for location updates in the background. Location updates in the background are required for designing navigation based applications which for example give turn by turn directions in both foreground and background and may even speak out the route changes. If an application developer wishes to receive location updates in the background, “UIBackgroundModes” tag with value “location” needs to be set in the application descriptor.

<iPhone>
    <InfoAdditions
        <![CDATA[
            <key>UIBackgroundModes<\key>
            <array>
                <string>location</string>
            </array>
        ]]>
    </InfoAdditions>
</iPhone>

If UIBackgroundModes is set to location  then AIR application would continue to receive location updates when application goes to background (event listener for AIR Geo-location APIs should be already added). In order to conserve battery life, all screen updates would be disabled and application would be throttled down to 4 fps in the background. Location services should be used judicially as the location APIs consume significant battery.
 
In case application developer want to design an application that can speak out the route changes in the background, “UIBackgroundModes” tag with value “location and “audio” needs to be set in the application descriptor.
 

<iPhone>
    <InfoAdditions
        <![CDATA[
            <key>UIBackgroundModes<\key>
            <array>
                <string>location</string>
                <string<audio</string>
            </array>
        ]]>
    </InfoAdditions>
</iPhone>

 
Task completion in background
AIR on iOS provides support for executing short task in the background. Task completion in background is required for designing applications were end user starts a long running task like a photo upload or sending an email with attachments and then enters the background. End user would expect that the initiated task would be completed in the background.
 
If an application developer want to execute short task in the background, executeInBackground property needs to be set to true.

NativeApplication.nativeApplication.executeInBackground = true;

One can also query the value of executeInBackground property

trace(NativeApplication.nativeApplication.executeInBackground);

Setting executeInBackground property to “true” does not guarantee that application would run in the background for infinite amount of time. iOS imposes an limit on the time up-to which an background task could run, application will be suspended once this time duration has lapsed. If value of executeInBackground property is set to “false”, application will be suspended on entering the background.
 
Consider a scenario where user starts a image upload operation and application goes to background before complete event is received. In deactivate event handler, application developer can set the executeInBackground property to “true” in case application developer wants upload operation to continue in the background. Whenever complete event is received by the application, executeInBackground property should be set to false, indicating application no longer want to execute in the background.
 
iOS does not guarantee that application would run in the background for infinite amount of time, there is a possibility that application would be suspended before it’s done with background processing. In this case NativeApplication.Suspend event would be dispatched to the app when it is about to be suspended, so that application developer can save its state. In order to conserve battery life, all screen updates would be disabled and application would be throttled down to 4 fps in the background.
 
NativeApplication.nativeApplication.executeInBackground property is applicable only on iOS and will fail silently, in case it is not supported on the underlying platform.
 
Opt out of Background Execution
AIR on iOS provides support for opting out of background execution. Opting out of background execution is required for designing applications that saves its state periodically and hence does not want to be suspended when it enters background. If an application developer wishes to opt out of background execution, “UIApplicationExitsOnSuspend” key with value “true” needs to be set in the application descriptor.

<iPhone>
    <InfoAdditions
        <![CDATA[
            <key>UIApplicationExitsOnSuspend<\key>
            <true/>
        ]]>
    </InfoAdditions>
</iPhone>

When UIApplicationExitsOnSuspend key is set to true, application is terminated and purged from memory instead of suspending when application moves to background. If UIApplicationExitsOnSuspend key is not present, or is set to false, the application moves to the background as usual.