PhoneGap-navigator.app.exitApp()无法正常工作 [英] PhoneGap - navigator.app.exitApp() Not Working

查看:115
本文介绍了PhoneGap-navigator.app.exitApp()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Phonegap制作一个小型应用程序,但是navigator.app.exitApp()?根本无法正常工作.

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

  • 这是我的第一个混合应用程序.
  • 我的目标平台是Android 5
  • 我正在使用Cordova CLI在Windows上进行开发.

我以此调用JavaScript函数

I call a JavaScript function with this

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

JavaScript:

JavaScript:

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

想法?

推荐答案

过去,调用navigator.app.exitApp()仅有几个绊脚石,但现在Google和Apple都为开发人员带来了主要障碍.

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. 确保您等待 事件,然后调用 exit .您可能会考虑放置一个启动屏幕,或将按钮或其他内容变灰(禁用),直到触发deviceready并加载Cordova库为止.
  2. 这是*障碍*.现在,您需要添加 whitelist 插件,对于Android,请添加. CSP需要该插件.您可以通过将所有Javascript(包括任何on*=)和<style>(和style=)移动到单独的文件中来解决此问题. CSP的例外-使用任何在线资源.
  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.

在#1上,

将此添加到您的javascript中:

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();
    });
}

将此添加到您的index.html:

Add this to your 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

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

注意,您的应用现在不安全.保护您的应用程序完全取决于您.
当您准备更加安全时,此白名单工作表应该会有所帮助.
如何:将Cordova/Phonegap应用到白名单系统

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

这篇关于PhoneGap-navigator.app.exitApp()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆