Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova Detect if user is leaving app or starting call (iOS)

Tags:

ios

cordova

I recently discovered Cordova, and started building an app that i've been thinking of. I need to figure out if i, through Cordova, can detect if the user is leaving the app (not locking the phone), or e.g. picking up a phone call, is there an event for this that i can bind to? I've searched through the most npm-library packages and did a lot of research but can't really find a good answer.

like image 391
Fredrik Avatar asked Jan 17 '26 13:01

Fredrik


1 Answers

Cordova providing Pause and resume events.

pause

The pause event fires when the native platform puts the application into the background, typically when the user switches to a different application. I think this will help you.

eg:

document.addEventListener("pause", onPause, false);

function onPause() {
    // Handle the pause event
}

resume

The resume event fires when the native platform pulls the application out from the background.

iOS Quirks

Any interactive functions called from a pause event handler execute later when the app resumes, as signaled by the resume event. These include alerts, console.log(), and any calls from plugins or the Cordova API, which go through Objective-C.

active event

The iOS-specific active event is available as an alternative to resume, and detects when users disable the Lock button to unlock the device with the app running in the foreground. If the app (and device) is enabled for multi-tasking, this is paired with a subsequent resume event, but only under iOS 5. In effect, all locked apps in iOS 5 that have multi-tasking enabled are pushed to the background. For apps to remain running when locked under iOS 5, disable the app's multi-tasking by setting UIApplicationExitsOnSuspend to YES. To run when locked on iOS 4, this setting does not matter.

resume event

When called from a resume event handler, interactive functions such as alert() need to be wrapped in a setTimeout() call with a timeout value of zero, or else the app hangs. For example:

document.addEventListener("resume", onResume, false);
function onResume() {
    setTimeout(function() {
            // TODO: do your thing!
        }, 0);
}
like image 55
Arjun T Raj Avatar answered Jan 20 '26 04:01

Arjun T Raj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!