我几周来一直在调试使用持久数据......我想我终于找到了问题的一部分。
我的大部分测试都是在Windows 10上的Chrome 59中运行浏览器平台,但我还使用Phonegap Build构建我的应用程序,并将其安装在我的iPhone(iOS 10.3.2)上。
根据Cordova doc's 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();
config.xml中
<?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
答案 0 :(得分:1)
在仔细阅读问题之后更新了答案:)
文件插件链接提到:&#34; Chrome文件系统在设备就绪事件后没有立即就绪。&#34;在您提供的代码示例中,您将进入&#34; deviceready&#34;事件,但正如文档提到的那样,它不适用于谷歌浏览器,但适用于iOS(Safari浏览器)。稍微调整我的原始答案:
尝试同时注册&#34; filePluginIsReady&#34;和&#34; deviceready&#34;尽快将事件附加到&#34; onload&#34; HTML正文标记的方法:
<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>