Cordova iOS don't对触摸输入做出响应

Cordova iOS don't respond to touchinput

本文关键字:输入 触摸 响应 don iOS Cordova      更新时间:2023-09-26

我在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>有一个内联javascript元素。从Cordova 5.0.0开始,您不能使用它。该限制是新WebView库的一部分(对于iOSUIWebviewWKWebview

要在HTML中使用内联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';">

请注意,您的应用程序现在不安全

保护应用程序的安全取决于您

以下链接将帮助您完成所需内容:
如何应用Cordova/Phonegap白名单系统
好运

onClick实际上需要是onclick。这会让你警觉起来,但只有当你抬起手指时,它才会变得迟缓。这是因为onclick通常用于计算机。对于带有cordova的ios,您可以使用ontouchstart