我在 iOS 上创建了一个 Cordova 应用程序,当我尝试在模拟器或真实设备上按下按钮时,应该会向我显示按钮已被按下的警报。不幸的是什么也没发生,我不太清楚为什么。一些代码:
<html>
<head>
<!--
Customize this policy to fit your own app's needs. For more guidance, see:
https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
Some notes:
* gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
* Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
* Enable inline JS: add 'unsafe-inline' to default-src
-->
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<title>Hello World</title>
</head>
<body>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
document.addEventListener('deviceready', function(){alert('device ready');}, false);
function showAlert(){
alert("Button is pressed!");
}
</script>
<h1>This is an app</h1>
<button onClick="showAlert()">Press me!</button>
</body>
谁能看出我是否犯了一个令人难以置信的愚蠢错误?
最佳答案
如果您使用的是 5.0.0 或更高版本,则需要使用 whitelist
系统。它涵盖了CSP
(内容安全策略),阻止内联 javascript 工作。
您的 HTML 元素 <button onClick="showAlert()">Press me!</button>
有一个 inline-javascript 元素。从 Cordova 5.0.0 开始,您不能使用它。该限制是新 WebView 库的一部分(UIWebview 和 WKWebview for iOS
)
要在 HTML 中使用 inline-javascript,您必须编写 CSP 异常。注意:这会使您的应用不安全。您可以通过下面的链接添加安全性。
这是您的代码,将其添加到您的 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';">
请注意您的应用现在不安全。
您可以自行决定保护您的应用程序。
以下链接将帮助您完成所需的操作:
HOW TO apply the Cordova/Phonegap the whitelist system
祝你好运
关于javascript - Cordova iOS 不响应 touchinput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33828827/