我正在使用Phonegap来创建一个小应用程序,但是navigator.app.exitApp()?根本不起作用。
我用以下方式调用一个JavaScript函数
<input type='button' onclick='exitApp();'/>JavaScript:
function exitApp() { navigator.app.exitApp(); }想法??
发布于 2016-02-03 06:02:39
过去称navigator.app.exitApp()只是几个绊脚石,但现在谷歌和苹果都给开发者带来了很大的障碍。
deviceready事件。您可能会考虑设置一个启动屏幕,或者在deviceready触发并加载Cordova库之前,将按钮或其他东西变灰(禁用)。whitelist插件,而对于Android则需要添加CSP。插件是CSP所必需的。您可以通过将所有Javascript (包括任何on*=)和<style> (和style=)移动到一个单独的文件来解决这个问题。CSP异常--使用任何联机资源。On #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>On #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';">注意到你的应用程序现在不安全了。保护应用程序的安全取决于你。。
当您准备好更安全时,此白名单工作表应该会有所帮助。
发布于 2016-02-02 04:39:40
使用以下方法:
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发布于 2016-02-02 05:29:42
您还可以在设备准备回调中添加一个侦听器。
onDeviceReady: function () {
document.addEventListener('backbutton', function(e){
e.preventDefault();
//TODO: throw up your dialog here!
}, true);
//other stuff here
}https://stackoverflow.com/questions/35144829
复制相似问题