Previously, using navigator.app.exitApp()
was straightforward with a few challenges, but now Google and Apple have introduced significant obstacles for developers.
- Ensure that you wait for the
deviceready
event before calling the exit function. Consider displaying a splash screen or disabling the button until deviceready
is triggered and the Cordova library is fully loaded.
- This is the current hurdle. It is now necessary to include a
whitelist
plugin and implement CSP
for Android. The plugin is essential for enforcing CSP
. One workaround is moving all JavaScript (including any on*=
) and <style>
(and style=
) code into a separate file, except for using external online resources in relation to CSP
.
Regarding #1,
Add this JavaScript code:
// 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>
For #2, a quick solution is:
Add the following lines 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 THAT YOUR APP IS NOW INSECURE. IT IS YOUR RESPONSIBILITY TO SECURE YOUR APP.
Include the following in 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 THAT YOUR APP IS NOW INSECURE. IT IS YOUR RESPONSIBILITY TO SECURE YOUR APP.
This guide on implementing the whitelist system will be beneficial once you are prepared to enhance the security of your app.
HOW TO: apply the Cordova/Phonegap the whitelist system