【问题标题】:deviceready not firing in Cordova app on iOS在 iOS 上的 Cordova 应用程序中,deviceready 未触发
【发布时间】:2018-04-15 00:44:29
【问题描述】:

我在 index.html 中添加了 deviceready 事件侦听器到脚本标记。但它不会在加载 HTML 时触发。相反,当单击主页按钮时,它会从 CDVViewController 中的 onAppDidEnterBackground 方法触发。

我想调用我的自定义插件来获取我试图在加载的 HTML 中填充的值。我发现很少有解决方案要求更改元标记。我确实尝试过改变,但没有用。它也不适用于iOS9。我猜元标记问题来自iOS10。我不确定我在这里缺少什么。

科尔多瓦 v4.4.0

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: http://* 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<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">
    <title>My HTML Document</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
  <script type="text/javascript" src='cordova.js'></script>
    <script type="text/javascript" src='CustomPlugin.js'></script>
        <script>
        document.addEventListener('deviceready', function () {
            window.plugins.CustomPlugin.myMethod(function (result) {
                document.getElementById('Name').value = result['Name'];
            }, function (error) {
                alert(error);
            });
        }, false);
    </script>
</head>
<body>
    <div class='article'>
            <div class='tableWrapper'>
                <table>
                <tr>
                    <td class='right'>CName:</td>
                    <td colspan='3'><input type='text' id='Name'/></td>
                </tr>
                </table>
            </div>

    </div>

</body>
</html>

【问题讨论】:

  • 尝试使用警报来测试事件是否触发。
  • 不。它没有。当我点击主页按钮时它会触发..

标签: javascript html ios cordova


【解决方案1】:

如果您遇到与我相同的问题,那么这与您的 content-security-policy 元标记有关。我用了this answer下面的那个,问题解决了。

<meta http-equiv="Content-Security-Policy" content="default-src 'self' data:* gap://* tel:* 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'" />

【讨论】:

    【解决方案2】:

    将所有脚本加载到 html 正文的底部

    <html lang="en">
    <head>...</head>
    <body>
      <div>Your content here</div>
      ...
      <script type="text/javascript" src='CustomPlugin.js'></script>
      <script type="text/javascript" src='cordova.js'></script>
      <script type="text/javascript" src='main.js'></script>
    </body>
    </html>
    

    并创建 main.js 文件,然后将您的代码放在那里..

    window.onload = function(){
        document.addEventListener("deviceready", firstInitCordova, true);
    };
    
    function firstInitCordova(){
        if(!window.cordova){
            // If cordova is not defined then call this function again after 2 second
            setTimeout(firstInitCordova, 2000);
            return;
        }
    
        //console.log(window.plugins);
        window.plugins.CustomPlugin.myMethod(function (result) {
            document.getElementById('Name').value = result['Name'];
        }, function (error) {
            alert(error);
        });
    }
    
    // If you're unsure then set a timer
    setTimeout(function(){
        firstInitCordova();
    }, 3000);
    

    【讨论】:

    • 我也试过你的方法。 firstInitCordova 仅在进入后台和进入前台后调用。
    【解决方案3】:

    尝试将您的 addEventListener 放入函数 onLoad 并在您的主体标签中使用 onload 事件调用它:

        function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
        }
    
        function onDeviceReady(){
               //the code you want to execute
        }
    

    <body onload="onLoad()">
    

    因为如果您按照自己的方式进行操作,您会尝试在加载 cordova 库之前执行 deviceready 事件。

    【讨论】:

    • 也试过了。未调用 onLoad 函数,但未触发 deviceready 事件。 :(
    • 如果你把你的脚本 javascriptscordova.js 放在最后一个位置怎么办,因为它在你的 customplugin.js 之前,当 onload 事件触发时,cordova.js 可能会被加载,但你的 customplugin 不会。 js
    • 我尝试在 customplugin.js 之后添加 Cordova.js。它不起作用。
    • 尝试在您的项目目录中执行该命令 "cordova platform remove ios" "cordova platform add ios"
    • 如果出现问题,它根本不应该调用 deviceready 事件吗?它在失去焦点时触发,例如按主页按钮、长按打开 Siri 等,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    相关资源
    最近更新 更多