问题
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.
- 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. - This is the *impediment*. You now need to add a whitelist plugin and for Android add
CSP
. The plugin is required forCSP
. You can get around this by moving all Javascript (including anyon*=
) and<style>
(andstyle=
) into a separate file. EXCEPTION forCSP
– 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