PhoneGap - navigator.app.exitApp() Not Working

核能气质少年 提交于 2019-12-19 19:58:08

问题


I'm using Phonegap to make a small application but the navigator.app.exitApp()? isn't working at all.

  • This is my first hybrid app.
  • My target platform is Android 5
  • I'm developing on Windows with Cordova CLI.

I call a JavaScript function with this

<input type='button' onclick='exitApp();'/>

JavaScript:

function exitApp() { navigator.app.exitApp(); }

Ideas??


回答1:


it used to be that calling navigator.app.exitApp() had just a few stumbling blocks, but now both Google and Apple have thrown in major impediments for developers.

  1. Make sure your you wait for the deviceready events before you make the call to exit. You might consider putting up a splash screen, or greying out (disableing) the button or something until deviceready fires and the Cordova library is loaded.
  2. This is the *impediment*. You now need to add a whitelist plugin and for Android add CSP. The plugin is required for CSP. You can get around this by moving all Javascript (including any on*=) and <style> (and style=) into a separate file. EXCEPTION for CSP – using any online resources.

On #1,

Add this to your javascript:

// Wait for PhoneGap to load
document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    // alert("deviceready");
    document.getElementById('exitApp').addEventListener('click', function() {
        navigator.app.exitApp();
    });
}

Add this to your index.html:

<button id="exitApp">Exit</button>

On #2, the quick answer is:

Add this to your config.xml

<plugin name="cordova-plugin-whitelist" source="npm" spec="1.1.0" />
<allow-navigation href="*" />
<allow-intent href="*" />
<access origin="*" /> <!-- Required for iOS9 -->

NOTE YOUR APP IS NOW INSECURE. IT IS UP TO YOU TO SECURE YOUR APP.
Add the following to your index.html

<meta http-equiv="Content-Security-Policy" 
         content="default-src *; 
                  style-src * 'self' 'unsafe-inline' 'unsafe-eval'; 
                  script-src * 'self' 'unsafe-inline' 'unsafe-eval';">

NOTE YOUR APP IS NOW INSECURE. IT IS UP TO YOU TO SECURE YOUR APP.
This whitelist worksheet should help when you are ready to be more secure.
HOW TO: apply the Cordova/Phonegap the whitelist system




回答2:


Use the following:

function backKeyDown() {
    navigator.notification.confirm("Are you sure you want to exit?", onConfirm, "Please Confirm", "Yes,No"); 
}
function onConfirm(button) {
    if(button==2){//If User selected No, then we just do nothing
        return;
    }else{
        navigator.app.exitApp();// Otherwise we quit the app.
    }
}

You have to install the following plugin:

cordova plugin install org.apache.cordova.dialogs



回答3:


you can also just add a listener in your device ready callback

onDeviceReady: function () {

    document.addEventListener('backbutton', function(e){
        e.preventDefault();
        //TODO: throw up your dialog here!
    }, true);

    //other stuff here
}



回答4:


Just use wherever you need in this line (Ionic 3 exactly working)

navigator.app.exitApp();

thats all. enjoy your coding...



来源:https://stackoverflow.com/questions/35144829/phonegap-navigator-app-exitapp-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!