0

几周以来,我一直在调试持久数据的工作……我想我终于找到了问题的一部分。

我的大部分测试都是在 Windows 10 上的 Chrome 59 中运行浏览器平台,但我也在使用 Phonegap Build 构建我的应用程序,并将其安装在我的 iPhone (iOS 10.3.2) 上。

根据Cordova 文档的 Chrome querks

设备就绪事件后 Chrome 文件系统未立即就绪。作为一种解决方法,您可以订阅 filePluginIsReady 事件。例子:

window.addEventListener('filePluginIsReady', function(){ console.log('File plugin is ready');}, false);

您可以使用window.isFilePluginReadyRaised函数来检查事件是否已经引发。

我无法让 'filePluginIsReady' 事件监听器工作,所以最后尝试了 console.logging window.isFilePluginReadyRaised,果然是假的!

无奈之下,我尝试添加一个计时器

setTimeout(function(){                        
     console.log('filePluginIsReady: ' + window.isFilePluginReadyRaised());

     consol.append(document.createElement("br"));
     consol.append('filePluginIsReady: ', window.isFilePluginReadyRaised());
 }, 5000);

我得到了真实的!我尝试将它降低到 1000 毫秒,它仍然可以工作,但是没有超时,我会出错。我将控制台日志附加到一个 dom 元素,这样我就可以在我的手机上调试它,这给我带来了问题:我什么也没得到......

我得到漂亮的绿色“设备准备就绪”,但五秒钟后,我的“控制台”仍然空白。没有结果window.isFilePluginReadyRaised()

我的 HTML

<div class="app">
    <h1>Apache Cordova</h1>
    <div id="deviceready" class="blink">
        <p class="event listening">Connecting to Device</p>
        <p class="event received">Device is Ready</p>
    </div>

    <h3>Console:</h3>
    <pre id='consol'></pre>
</div>

<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="dist/build.js"></script>

JS

var app = {
    initialize: function () {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    onDeviceReady: function () {
        this.receivedEvent('deviceready');

        setTimeout(function(){                        
            console.log(cordova.file);

            console.log('filePluginIsReady: ' + window.isFilePluginReadyRaised());

            consol.append(document.createElement("br"));
            consol.append('filePluginIsReady: ', window.isFilePluginReadyRaised());
        }, 5000);
    },

    receivedEvent: function (id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
};

app.initialize();

配置文件

<?xml version='1.0' encoding='utf-8'?>
<widget id="io.cordova.hellocordova" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloCordova</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" />
    <preference name="iosPersistentFileLocation" value="Library" />
    <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:*" />
    <platform name="android">
        <allow-intent href="market:*" />
    </platform>
    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
    <engine name="browser" spec="^4.1.0" />
    <plugin name="cordova-plugin-file" spec="^4.3.3" />
    <plugin name="cordova-plugin-whitelist" spec="^1.3.2" />
</widget>

编辑:我发现我的文件结构可能不正确。Phonegap 指定配置应该在 www 目录中,而我的是一个级别。解决此问题并不能解决问题。我还检查了 Phonegap Build 日志:

--------------------------------------------------------------------------------
PLUGIN OUTPUT
--------------------------------------------------------------------------------
Fetching plugin "cordova-plugin-file@^4.3.3" via npm
Installing "cordova-plugin-file" at "4.3.3" for ios
Fetching plugin "cordova-plugin-compat" via npm
Installing "cordova-plugin-compat" at "1.1.0" for ios
Fetching plugin "cordova-plugin-whitelist@^1.3.2" via npm
Installing "cordova-plugin-whitelist" at "1.3.2" for ios
4

1 回答 1

1

更新了答案,在仔细阅读了问题之后:)

文件插件链接提到:“Chrome 文件系统在设备就绪事件后没有立即就绪。” 在您提供的代码示例中,您正在连接“deviceready”事件,但正如文档所述,这不适用于 Google Chrome,但应该适用于 iOS(Safari 浏览器)。稍微调整一下我的原始答案:

尝试尽快注册“filePluginIsReady”和“deviceready”事件,将其附加到 HTML 正文标记的“onload”方法:

    <body onload="initialize();">
         <div class="app">
             <!-- all other html -->
         </div>
         <script type="text/javascript">
              function onFilePluginReady() {
                     // use file plugin
                     console.log("onFPReady: " + cordova.file);
              }
              function onDeviceReady() {
                     // use file plugin - iOS or other mobile platforms
                     console.log("onDeviceReady: " + cordova.file);
              }
              function initialize() {
                  // special event for Google Chrome (Android)
                  document.addEventListener('filePluginIsReady', onFilePluginReady, false);
                  // event for iOS and others
                  document.addEventListener("deviceready", onDeviceReady, false);

              }
         </script>
    </body>
于 2017-06-27T23:29:45.230 回答