【问题标题】:Cordova inappbrowser deviceready never firesCordova inappbrowser deviceready 永远不会触发
【发布时间】:2015-12-07 13:29:43
【问题描述】:

我有一个 cordova 项目,我需要使用 inappbrowser 插件打开一个窗口,其中包含一个 Cordova 页面,而“父级”仍在运行。

Cordova 使用 inappbrowser 插件加载 index.html,打开 frame.html
Cordova 已加载到 frame.html,但 deviceready 事件从未触发,5 秒后我在 frame.html 的控制台中收到此错误。

这只发生在使用 inappbrowser 插件打开的第二个 webview 中。

错误:

cordova.js:1183 deviceready has not fired after 5 seconds.
cordova.js:1176 Channel not fired: onFileSystemPathsReady
cordova.js:1176 Channel not fired: onCordovaInfoReady

我进行了很多搜索,得出的结论是,并不是文档的 onload 中的插件或代码过多会延迟触发 deviceready 所需的 onNativeReady 事件。
当我删除 cordova-plugin-filecordova-plugin-file-transfer 时,onFileSystemPathsReady 错误消失了。 如果我删除 cordova-plugin-deviceonCordovaInfoReady 错误就会消失。

但我需要那些插件。
是什么导致 deviceready 事件根本不触发?
在不删除我需要的插件的情况下,我该怎么做才能让它启动?

项目信息:

Node version: v4.2.2
Cordova version: 5.4.1

Installed platforms:
    android 4.1.1
    ios 3.9.2

Installed plugins:
    cordova-plugin-bluetoothle 2.4.0 "Bluetooth LE"
    cordova-plugin-device 1.1.0 "Device"
    cordova-plugin-dialogs 1.2.0 "Notification"
    cordova-plugin-file 3.0.0 "File"
    cordova-plugin-file-transfer 1.4.0 "File Transfer"
    cordova-plugin-inappbrowser 1.1.0 "InAppBrowser"
    cordova-plugin-vibration 2.0.0 "Vibration"
    cordova-plugin-whitelist 1.2.0 "Whitelist"
    de.appplant.cordova.plugin.background-mode 0.6.4 "BackgroundMode"  

Config.xml file:
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.test.app" version="0.0.1" xmlns="http://www.w3.org/ns/widgets">
    <name>TestApp</name>
    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="dev@cordova.apache.org" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <allow-navigation href="http://*/*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

index.html:

<!DOCTYPE html>
<html >
    <head >
        <meta http-equiv="Content-Security-Policy" content="default-src * data: gap: https://ssl.gstatic.com 'unsafe-eval' 'unsafe-inline';" >
        <meta name="format-detection" content="telephone=no" >
        <meta name="msapplication-tap-highlight" content="no" >
        <meta name="viewport" content="user-scalable=no, initial-scale=1, height=device-height, width=device-width" >
        <title >Index</title >
        <script >
            document.addEventListener( 'deviceready', function ()
            {
                console.log( 'ready' );
                win = window.open( 'frame.html', '_blank', 'location=no', function()
                {
                    console.log( arguments );
                } );
            }, false );
        </script >
    </head >
    <body >
        <script type="text/javascript" src="cordova.js" ></script >
    </body >
</html >

frame.html:

<!DOCTYPE html>
<html >
    <head >
        <meta http-equiv="Content-Security-Policy" content="default-src * data: gap: https://ssl.gstatic.com 'unsafe-eval' 'unsafe-inline';" >
        <meta name="format-detection" content="telephone=no" >
        <meta name="msapplication-tap-highlight" content="no" >
        <meta name="viewport" content="user-scalable=no, initial-scale=1, height=device-height, width=device-width" >
        <title >Frame</title >
        <script >
            window.addEventListener( 'deviceready', function()
            {
                console.log( 'window.deviceready', arguments );
            }, false );
            document.addEventListener( 'deviceready', function()
            {
                console.log( 'document.deviceready', arguments );
            }, false );
        </script >
    </head >
    <body >
        <script type="text/javascript" src="cordova.js" ></script >
    </body >
</html >

更新:

我正在使用两个自我修改的插件(不是导致问题的插件),也许这是某种相关的:

编辑:

我需要使用 inappbrowser 插件,因为蓝牙插件必须在另一个框架中浏览应用程序时保持连接。 框架还需要能够相互通信,并且都可以访问cordova库。

如果有人有其他想法如何做到这一点,我愿意接受。

更新:

我正在研究 cl.kunder.webviewhttps://github.com/kunder-lab/cl.kunder.webview 插件

【问题讨论】:

  • "deviceready" 只会在您的 index.html 首次加载时调用一次。如果您想使用 InAppBrowser 加载事件,我想您需要使用 InAppBrowser 事件,例如“loadstart、loadstop、loaderror、exit”。不确定这是您正在寻找的解决方案,但据我了解,这就是您想要的。
  • @JDev cordova 记录了 2 个通道尚未触发。如果我删除使用这 2 个通道的插件,设备就绪 触发。我希望它在安装插件的情况下触发。这 2 个插件还没有完成加载,否则 deviceready 会触发

标签: javascript cordova


【解决方案1】:

@ThisNameBetterBeAvailable,

InAppBrowser 没有可用的 Cordova 服务。

来自文档(第 #3 段)我引用

InAppBrowser 窗口的行为类似于标准 Web 浏览器,无法访问 Cordova API。

这意味着没有插件,没有事件,也没有服务。我试图让他们修复这部分文档,但这需要一段时间。

你可以在这里看到它:https://issues.apache.org/jira/browse/CB-9470

【讨论】:

  • 我认为自己添加这些cordova API 太难了?或者是否可以分叉并添加cordova API?
  • 我之前使用的plugin 似乎能够访问cordova API,只是我无法在创建的webview 和启动它的webview 之间进行任何通信。
  • 感谢插件上的指针。截至目前,Cordova 插件存储库中有 13 个webview alternatives;许多是一个或两个基本来源的克隆。我试着保持一个“官方”webview图书馆的博客,他们的detailsalternatives
  • Cordova 插件的常见替代方案是 HTML5 API。根据平台,您可以访问 GPS(地理位置)、相机、本地存储等。关于您的问题,如果您的目标只是蓝牙,您应该重新考虑您的设计,并可能使用 SPA(单页应用程序)或类似的东西。考虑到替代品,添加是困难的。您可以随时分叉,并将其用作私有插件。但是,您指向的插件违反了当前所有的“安全”更改; Google、Apple 或 Phonegap 不允许这样做。
  • 从网站获取网页被认为是危险的 - 并且在它和应用程序之间创建了一条线。要使用 Cordova/Phonegap,您需要添加白名单。这个 whitelist 工作表应该会有所帮助。 HOW TO apply the Cordova/Phonegap the whitelist system 祝你好运
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 2017-07-05
相关资源
最近更新 更多