Shazron's Cordova (aka PhoneGap) Blog

at Adobe Systems Inc.

Apache Cordova lifecycle events in iOS 4 versus iOS 5

with 9 comments

Cordova has lifecycle events – “pause” and “resume” which are documented in http://docs.phonegap.com but there are two more events for iOS that are undocumented, “resign” and “active”. These events correspond to when the app is leaving the active state (UIApplicationWillResignActiveNotification) and when the app enters the active state (UIApplicationDidBecomeActiveNotification), respectively.

In iOS 4, the “resign” and “active” events can be used to detect when the Wake/Lock button has been pressed exclusively, but in iOS 5, this has slightly changed. In iOS 5, the “pause” (UIApplicationDidEnterBackgroundNotification) and “resume” (UIApplicationWillEnterForegroundNotification) events are triggered as well (see  the section Moving to the Background) BUT only if your app’s UIApplicationExitsOnSuspend setting in your Info.plist is NO (i.e. multi-tasking is enabled) .

This in essence tells devs that apps when in the “locked” state are put into the background as well if the app is allowed to multi-task in iOS 5, while in iOS 4, locking will never put your app into the background, ever.

iOS devices that do not support multitasking (the iPhone 3G and iPod Touch 2nd Generation – even under iOS 4), and apps that set their UIApplicationExitsOnSuspend setting in their app’s Info.plist to YES (i.e. no multi-tasking), will never get the “pause” and “resume” events, of course.

tldr; So – if you want to have your app still in the foreground running when your device is locked – and you are running iOS 5 – you will have to DISABLE multi-tasking for your app (UIApplicationExitsOnSuspend set to YES).

9 Responses

Subscribe to comments with RSS.

  1. same behavior when you receive a call?

    Giacomo Balli

    March 15, 2012 at 7:12 am

  2. Really struggling with this. I’ve added a line to the config.xml, , yet it still goes into a suspended state when locking or navigating to another app. I’m able to fire code from the onResign event, but it doesn’t seem to allow for setTimeOut or setInterval. So far I’ve written a loop which does work, but I need to be able to track the gps location every 5s while the app is in the background. Any suggestions? It works perfectly on Andoid but not on iOS.

    afreekdusoet

    May 17, 2012 at 4:11 pm

  3. How is this set in the config.xml for PhoneGap Build? Is the following correct?

    Is the following also required for location /gps montitoring to continue in background?

    afreekdusoet

    May 17, 2012 at 7:26 pm

  4. — preference name=”UIApplicationExitsOnSuspend” value=”YES” / —

    — preference name=”UIBackgroundModes” value=”location” / —

    afreekdusoet

    May 17, 2012 at 7:26 pm

  5. Is this still the case? My experiments seem to indicate not? Which version of Cordova was this article targeting?

    Jon R

    August 22, 2012 at 1:57 pm

    • It was occurring in 1.8, haven’t tested since then.

      shazron

      August 24, 2012 at 8:05 pm

    • It has nothing to do with Cordova btw, it’s dependent on the iOS versions, and I have tried this experiment multiple times, it does occur.

      shazron

      August 24, 2012 at 8:07 pm

      • Ahh I think I misread your blog post slightly;

        I’ve been looking at the background behaviour of the app whilst locked/suspended. (I never wanted foreground behaviour, just continued callbacks on location updates).

        My apologies.

        Jon R

        August 24, 2012 at 8:40 pm


Leave a comment