在iOS上的Cordova应用中无法启动deviceready [英] deviceready not firing in Cordova app on iOS

查看:175
本文介绍了在iOS上的Cordova应用中无法启动deviceready的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有index.html,其中在脚本标记中添加了deviceready事件侦听器.但这不是在加载HTML时触发的.而是单击主页按钮时,它是由CDVViewController中的onAppDidEnterBackground方法触发的.

I have index.html where I have deviceready event listener added to script tag. But it is not triggered on loading the HTML. Instead, when clicking the home button it is triggered from onAppDidEnterBackground method in CDVViewController.

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

I wanted to call my custom plugin to get the values which I am trying to populate in the HTML loaded. I found few solutions asking to change the meta tag. I did try changing, but no use. It is not working in iOS9 also. I guess the meta tag issue is from iOS10. I am not sure what I am missing here.

科尔多瓦v4.4.0

Cordova 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>

推荐答案

如果您遇到的问题与我相同,那么这与您的内容安全策略元标记有关.我使用了以下此答案中的一个,问题就解决了.

If you're having the same problem I was, then this is related to your content-security-policy meta tag. I used the one below from this answer, and problem solved.

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

这篇关于在iOS上的Cordova应用中无法启动deviceready的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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