Phonegap - navigator.app.exitApp () not working

I use Phonegap to create a small application, but it navigator.app.exitApp()?doesn’t work at all.

  • This is my first hybrid app.
  • My target platform is Android 5
  • I am developing Windows using the CLI Cordova.

I call a JavaScript function with this

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

JavaScript:

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

Ideas ??

+5
source share
4 answers

@Thomas,
It used to be that navigator.app.exitApp()there were only a few stumbling blocks in the call , but now both Google and Apple have posed serious obstacles for developers.

  • , deviceready, . , () - , deviceready, .
  • * *. whitelist, Android CSP. CSP. , Javascript ( on*=) <style> ( style=) . CSP – -.

. # 1,

javascript:

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

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

index.html:

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

# 2, :

config.xml

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

. .
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';">

. .
, .
: Cordova/Phonegap

+6

:

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.
    }
}

:

cordova plugin install org.apache.cordova.dialogs
+2

onDeviceReady: function () {

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

    //other stuff here
}
+1

, (Ionic 3 )

navigator.app.exitApp();

. ...

0

Source: https://habr.com/ru/post/1626949/


All Articles