Exit vs Suspend your iOS Application

June 24, 2011By 27 Comments

When developing for iOS, you should be aware of a setting that will allow you to completely exit your application rather than just suspend to the background (due to the iOS 4.0 multitasking features allowing you to move quickly between your applications). There’s a key that can be specified in your AIR app-descriptor.xml iPhone section to do this, that when set to true will inform the device that it should completely exit the application rather than suspend it (when the user hits the home button to go to another application for instance). The flag is called UIApplicationExitsOnSuspend and requires a Boolean value. The definition for this flag from the Apple docs says:

UIApplicationExitsOnSuspend (Boolean – iOS) specifies that the application should be terminated rather than moved to the background when it is quit. Applications linked against iOS SDK 4.0 or later can include this key and set its value to YES to prevent being automatically opted-in to background execution and application suspension. When the value of this key is YES, the application is terminated and purged from memory instead of moved to the background. If this key is not present, or is set to NO, the application moves to the background as usual. This key is supported in iOS 4.0 and later.

HOWEVER, one important thing to note is that when you’re setting this value in your app-descriptor, you need to use a value of true or false versus YES or NO because the YES/NO will resolve to a String based on the examples I’ve seen. Instead your app-descriptor needs to have this key included within the iPhone section such as the following:

<iPhone>
        <InfoAdditions><![CDATA[
			<key>UIDeviceFamily</key>
			<array>
			    <string>1</string>
				<string>2</string>
			</array>
			<key>UIApplicationExitsOnSuspend</key>
			<true/>
		]]></InfoAdditions>
        <requestedDisplayResolution>high</requestedDisplayResolution>
</iPhone>

A full list of the different iOS keys can be found here, but you will need to also check the Adobe documentation for reserved keys that cannot be defined in your app-descriptor…

Filed in: Adobe AIRFlash Builder 4.5Flex 4.5Flex 4.5/MobileFlex iOSMobile DevelopmentUncategorized Tags:

About the Author ()

Comments (27)

Trackback URL | Comments RSS Feed

Sites That Link to this Post

  1. To Kill or Pause an iOS App (when it’s Deactivated) | roguish | July 22, 2011
  1. Thx for this post. I couldn’t find any link to an official documentation :) Cool.

  2. David says:

    Excellent – just what I needed this afternoon!

    BTW, I need to save data changes back to my SQL database and want to avoid using a button in the UI to call my save method. Conceptually, it would be nice to interrupt the app closure process and return once my service call is complete.

    Any ideas on how to do this? Or, is generally accepted to have a ‘save’ button like desktop apps?

    I have tried to use the navigatorStateSaving event, but can’t get the async nature of my service call to work as the app has gone bye, bye. Obviously, the app plows forward with its shutdown directive. Seems to me this would have been a nice area to reconcile changes before shutdown.

    Also tried to use the stage.nativeWindow to listen for close (alas – it only works for desktop!)

    Any ideas are appreciated!

    Regards,

    David

    • David says:

      I may have answered my own question with some new thoughts and links this morning. Perhaps this will help stimulate some further ideas or help someone else!

      Background – I am learning Mobile Flex Development and desire to build a simple CRUD example that speaks to my PHP/MySQL backend. All service calls are complete in PHP and speak JSON. The CRUD example must have inherent intelligence to persist changes to the appropriate service calls, with out a ‘save’ button on the UI. As I have been learning, a mobile app can be shut down by the OS (call coming in) or the user can close it. Obviously a save button connected to a method that makes a service call would get the job done – just felt a little ‘old school’ for the mobile world and clearly does not answer forced shutdowns.

      So after reading this tutorial (http://www.adobe.com/devnet/flash/articles/saving_state_air_apps.html) and thinking with a fresh cup of coffee, I have decided to use a layered approach to data persistence.

      1.) Utilize UI interaction – for Create and Delete operations, this can easily be integrated at the moment a button is tapped, or (more easily) listen to the kind of change event on the array collection.

      2.) But what about Update – certainly don’t wan’t to fire a service call every time a keystroke occurs! So, here my thought is to capture the change kind called ‘Replace’ as when the user clicks the back button on my form (returning to the list), I am replacing the item in the collection with the navigator.poppedViewReturnedObject. This doesn’t capture navigating to another screen nor shutdown – more layers…

      3.) A timer at the application level could be used to sweep through my collections every 30-60 seconds and perform necessary service calls.

      4.) Finally, I can add an Exiting event listener to NativeApplication.nativeApplication to (potentially) capture any un-perssted stragglers. Would need to review this more.

      5.) May also need to think about service calls that fail due to network connection drop. Perhaps store locally and reconcile later when connection is available?

      Any other ideas out there? Would be wonderful if there was a class written to auto persist to online database instead of just LSO or SQL Lite.

  3. michael B says:

    Is there a way to force open the application IE: lock the interface ?

  4. Thanks for this, the setting does work as indicated.

    One thing I’d like to be able to do is to somehow close my app on iOS.

    NativeApplication.nativeApplication.exit() does not close the app on iOS. It does work on Android.

    What I want to do is be able to close my app and go back to Home on iOS!.

    Is there a way to do that? Could not find anything on the documentation. All I got from various places was that closing an app is not in Apple’s IOS HIG!

    Well, on a corporate environment, where you might be dealing with secure apps, when a user logs out of my app, I want it to actually close, independently of what Apple says :-(

    cheers,
    julio

  5. kathryn says:

    hey holly, do you know of a way to keep the app running in the background, rather than suspended? i have an app with a timer that needs to keep counting down and ding on complete even if it’s in the background.

    i notice that a timer app i bought does not ding in the background, but on resume shows a dialog to the effect that the timer ended while outside the application. so maybe it’s not possible in native ios apps either…

  6. Holly, thank you for this useful article. I don’t know before about this. also thanks for official documentation you shared, never know its exist before.

    Anyway, I found exiting event cannot be catched in IOS (work in Android). Could you suggest some guideline to catch user exiting event in iOS.

    // Work in Android
    NativeApplication.nativeAp​plication.addEventListener​(Event.EXITING,onExiting);

  7. Ricardo Faria says:

    Hi Holly.

    Can an App have both Exit and Suspend? I sometimes need to really exit and other times just suspend…

    Thanks for the article by the way.

  8. Felix says:

    Hi Holly. I’ve created a game for iOS using Flash CS5 and I really need to know how can I make it work with Apple’s Game Center. Game Center features would add a huge amount of fun and challenge to the game. Any reference on to how to code that will be of great help!
    Thank you in advance!

  9. Amit says:

    Is there any way we can remove the default pushView() transition. as per my requirment i need to show a login page while while changing the tab. What i am doing is pushing the login view on selected tab’s wiew navigator object which shows the default transition. i need to show the login view directly without animating.

    Please help.

  10. Felix says:

    I got my response form another post. I guess it’s not possible to integrate external APIs like the Game Center one.
    But I have another issue: When i publish my app with icons integrated the iTunes does not synch the app: replies with “Unknown Error:0xE800004C”

  11. Felix says:

    Any ideas why?

  12. Felix says:

    Got my response after searching. Sorry for asking!

  13. Robert C says:

    Hi,
    Interesting discussion.
    I have the tweek in my xml file, iPhone 4.xx byt the way, and so far, I can’t tell if anything actually happens. It seems not.
    xml file,

    <![CDATA[UIDeviceFamily

    1
    2

    UIApplicationExitsOnSuspend
    ]]>

    high

    and code in the app, called by a button;

    private function btnExitMouseClickHandler(event:Event):void
    {
    event.stopPropagation();
    NativeApplication.nativeApplication.exit();

    }

    What it doesn’t actually do is shut down the app – aka Android.

    It would seem that there is now way out of this problem to date. :-(

    • Hi Robert, since there really isn’t a concept of ‘exiting’ on iOS apps, the NativeApplication.nativeApplication.exit() will never work on iOS. The UIApplicationExitsOnSuspend flag simply says that when the app is going into suspend mode, make it exit instead. So for instance, if the user is running your application and hits the home button, or it otherwise gets disrupted, it would go into suspend mode by default unless this flag is set, then it will force an exit in that situation. I hope that helps! Holly

  14. Isaac Rivera says:

    Developing an iPad app on CS5 I have added the following info additions:

    <![CDATA[
    UIDeviceFamily
    2
    UIStatusBarHidden

    UIApplicationExitsOnSuspend

    ]]>

    The status bar seems to hide, correcting an issue that made the image resize in the middle of the splash screen. However, the UIApplicationExitsOnSuspend does not seem to do a thing. Unless I am expecting the wrong behavior… I am expecting my app to start all the way from the beginning, splash screen an all. What I get on the other hand is that the app comes back in exactly the same state as when it was minimized/suspended. Is this correct?

  15. Somedude says:

    This is not working on iOS, when the users hits the home button the application icon is still shown in the mulititask bar.

    So if the user can’t quit the application is there a way to send the application to suspend ?, then after setting this flag to true it will quit by itslef ? That would be most logical meaning of “please how can i just make this app to go away from my screen” programatically”

  16. Colin Holgate says:

    @Somedude, that row of icons on iOS isn’t a multitasking bar, it’s more of a recent bar. It shows the apps you’ve used, in the order that you last used them, regardless of whether they are open. You can tell if the exit on suspend worked by touching the icon from anywhere when the app isn’t running. If you get your splash screen, then the app wasn’t running. If you get one of the app’s own screens, then it was running.

    One small complication is that iOS seems to grab the image of the last screen you were on as it quits, and will show that while the app resumes. You can sometimes get the confusing sequence of that snapshot followed by the app’s start up sequence. You might end up thinking that the app crashed on resume, but it didn’t, the app really wasn’t open, and iOS was just messing with you.

  17. interesting articles says:

    ej jak stworzyć takie logo helpbay co pokazany jest obrazek na gurze niepytam się o film tylko o ten obrazek helpbay

  18. Alex Kap says:

    When you activate your app is there a way to know if it was suspended?

  19. Fahad says:

    Hi Experts
    My query about using this tag is regarding a return mechanism. If my app initiates a call on my iphone and after I hang up the call my app is in the background and I return to contact list. What if I want the phone to directly return to my App when the call is hung up.

  20. Deepak says:

    Hi there,
    I have iPad 2 and iOS version is 5.1.
    I have added the following code under application initialize:

    protected function application1_initializeHandler(event:FlexEvent):void
    {
    // TODO Auto-generated method stub
    NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, onDeactivateApp);

    }

    protected function onDeactivateApp(event:Event):void
    {

    NativeApplication.nativeApplication.exit();
    }

    And added ‘UIApplicationExitsOnSuspend’ key under InfoAdditions of iPhone, under app descriptor xml file:

    <![CDATA[
    UIDeviceFamily

    1
    2

    UIApplicationExitsOnSuspend

    ]]>
    high

    This worked perfectly fine for me. Application completely exits on hitting the ‘exit\home’ key of ipad. Next time I tap on the app, it initializes from beginning! Thanks for the post. Hope this helps others too.

    • Deepak says:

      Oops! since it’s xml based, i think it didn’t print it properly. Here it is once againm iPhone settings under app descriptor file:

      < ! [ CDATA [
      UIDeviceFamily

      1
      2

      UIApplicationExitsOnSuspend

      ] ] >
      high

  21. Danilo says:

    Guys! We can add a listener “Event.DEACTIVATE” with that listener can know if the user pressed the home button. But what we all want is one button inside our application, for example, “CLOSE”. When you press the button, it would simply close the application. Can we do it on AIR? Thanks ;)

  22. I found a developer who wore a while (true) that instantiated variables thus crowding 2048×2048 bitmap memory and killing the application. I found it a bit “clumsy”, but apparently works better than not allowing the application to happen in the background.

    Well in my opinion you AIR built a house and forgot the door.

Leave a Reply