This is my code
//This is an event that fires when a PhoneGap application is put into the background.
document.addEventListener("pause", onPause, false);
//This is an event that fires when a PhoneGap application is retrieved from the background.
document.addEventListener("resume", onResume, false);
// Handle the pause event
function onPause(){
console.log("pause : app is put into background");
}
// Handle the resume event
function onResume() {
console.log("resume : app is put into foreground");
}
When i press the home button there is no log in the console however when I click the app (make it in foreground) then my log is
2011-11-22 12:11:37.206 Event[644:207] [INFO] pause : app is put into background
2011-11-22 12:11:37.206 Event[644:207] [INFO] resume : app is put into foreground
I don't know why pause function is called when it comes in foreground.
Is there anything that I'm doing wrong?
This is from the docs
iOS Quirks
In the pause handler, any calls that go through Objective-C will not work, nor will any calls that are interactive, like alerts. This means that you cannot call console.log (and its variants), or any calls from Plugins or the PhoneGap API. These will only be processed when the app resumes (processed on the next run-loop).
I suspect what is actually happening is that the console.log() from the pause event is not so much being fired on resume, as it's just that the system cannot output your console.log() until it comes back.
The Objective-C method in PhoneGapDelegate.m that fires the pause event (applicationWillEnterForeground:(UIApplication *)application) sends it to the JavaScript but by then the app is in the background and suspended. The JavaScript cannot receive the event until it re-enters the foreground.
To test this, simply background your app for a longer period of time... it should then cause their error:
void SendDelegateMessage(NSInvocation*): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
This appears to be a bug in PhoneGap. Perhaps you could raise an issue at: https://github.com/callback/callback-ios ?
Related
Hey guys to provide some context to the issue, I have a timer function:
timer = Timer.periodic(
duration,
(timer) async {
currentSeconds = timer.tick;
if (timer.tick >= timerLowerBound) {
// execute async call back here
}
}
That makes an async call back to my backend to generate a Firebase notification to alert the user that the timer is up. The problem I'm facing is that I realize the async call does not execute when I have the app minimized. Once the app is back on the foreground, it will continue to execute. The behaviour only extends to async call, the timer continues to tick even tho the app is in the foreground.
Another thing I would like to point out is that this effect only happens in TestFlight, and does not occur when i run the app in a simulator / real device (via xcode).
Does the iOS app go to the background when we make the call using tel://123456789 ?
I have an iOS app that requires to log any outbound event happening through our app (like sending mail, calling, etc).
For Calling
we use some thing like :
in this case dialler opens up and makes the call but pause or resume events never gets
triggered . So basically we dont have hook/event after the call was ended to make the log
entry for call .
Though while replicating the same flow on android proper events(resume, pause) gets fired.
For Mail
we use some thing like :
in this case mail client of the user gets opened and our hybrid app goes to background and once mail is sent and the user returns to the app "resume" event gets fired
We need something similar for the phone call. Any pointers will be really appreciated.
You could try to listen to the click on the a tag then trigger that event yourself if it is not triggered by Cordova, that way you don't have to change your code. Using jQuery:
$('your_a_tag').on('touchstart', function(event){
$(document).trigger('pause');
});
Alternatively, you could open a support ticket on cordova-ios repo.
If I use this method to run a task, but keep app in foreground, will expirationHandler be called while app is in foreground?
I use this for starting location service everytime I enter background, but sometimes user enters background and immediately returns to app, will this call expirationHandler?
func beginBackgroundTask(expirationHandler handler: (() -> Void)? = nil) -> UIBackgroundTaskIdentifier
Documentation says:
A handler to be called shortly before the app’s remaining background time reaches 0. Use this handler to clean up and mark the end of the background task. Failure to end the task explicitly will result in the termination of the app. The system calls the handler synchronously on the main thread, blocking the app’s suspension momentarily.
https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio
It seems that expiration handler is not called in the foreground mode. After very long waiting I went into background mode and then after ~170 seconds expiration handlers were called for all reported previously background tasks.
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.
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);
}
I own an AngularJs app that I converted into a native app through Cordova.
The whole works on iOS (iphone), however I've just noticed that if I put the app on the background, then reopen/resume it, the application sometimes "freezes" for about 5 seconds then works.
No scrolling, no click events, for 5 seconds.
Any known reason to this? How to avoid it? It's very frustrating.
I found the code causing the issue:
document.addEventListener("resume", function () {
$rootScope.$broadcast('retrieveAllNotifications', null);
}, false);
So, according to the documentation:
Interactive functions like alert() when the resume event fires will
need to be wrapped in a setTimeout call with a timeout value of zero,
or else the app will hang. e.g.
So I transformed to:
document.addEventListener("resume", function () {
$timeout(function () {
$rootScope.$broadcast('retrieveAllNotifications', null);
}, 0);
}, false);
but it still hangs.
However, when I remove the broadcast, it works.
How to fix it? Increasing the timeout to 1 second for instance?
Actually, I noticed that I retrieved more than 500 notifications that are displayed (though hidden) in HTML.
What I've done is to delete a lot of notifications, and I noticed no more freeze.
It was just too heavy for the phonegap app to display 500 notifications at once without freezing.