简体   繁体   中英

Fire javascript function when app goes into background

My app has a timer that is supposed to fire when the app is in the background, however, due to ios quirks, my onPause function isn't fired until it the app is resumed. I have been researching ways to fire a javascript funciton from obj-C, but cannot seem to get it to work.

I have the following code

// CUSTOM CODE TO DETECT APP STATE
- (void)applicationDidEnterBackground:(UIApplication *)application {

NSString* jsString = [NSString stringWithFormat:@"onPause;"];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];

NSLog(@"OBJ-C paused");

}

My js code looks like so;

function onPause() {
            console.log("JS paused");
        }

When the app goes into the background, I can seem my OBJ-C log, but cannot get the js log and when i start the app back up, the timer has not paused.

Can anyone with help or advise me as to what I am doing wrong, I have no experience with OBJ-C and am using Phonegap 2.1.0.

Try to use applicationWillResignActive: .

The app probably doesn't react anymore if the app was already sent to background.

SOLVED!!! In the following link here . There are 2 undocumented events; resign and active which made it work as should for my purposes.

document.addEventListener("resign", resign, false);
document.addEventListener("active", active, false);

        function resign() {
            // pause code fired
        }

        function active() {
            //resume code fired
        }

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM