从Phonegap 2.5.0升级到2.9.0后,我无法再触发deviceready事件。
我尝试过的事情:
添加包含cordova_plugins.json
{}
文件
在计时器中查找window.device
(似乎从未初始化)(建议here)
从cordova.js
删除代替加载cordova_plugins.json
并替换为finishPluginLoading()
似乎没有任何效果。我把头发拉过这一头。请在我还有一些人的时候帮忙!
到目前为止,这是我的代码,但它经过了多次迭代,因此它包含一些显示我尝试过的其他途径的死代码:
$(document).ready(function() {
function initializePhoneGap( success, failure ) {
var timer = window.setInterval( function () {
if ( window.device ) {
window.clearInterval( timer );
success();
}
}, 100 );
window.setTimeout( function () { //failsafe
if ( !window.device ) { //phonegap failed
window.clearInterval( timer );
failure();
};
}, 10000 ); //5 seconds
}
console.log( 'Waiting for launch...');
if (navigator.userAgent.match(/(iPhone|iPod|iPad|Android|BlackBerry|IEMobile)/)) {
//$(document).on("deviceready", didFinishLaunching, false);
document.addEventListener("deviceready", didFinishLaunching, false);
//initializePhoneGap( function(){ console.log('Phonegap initialized'); didFinishLaunching() }, function(){ console.log('Phonegap timed out'); didFinishLaunching() } );
} else {
console.log('Skipping phonegap initialization');
didFinishLaunching();
}
function didFinishLaunching() {
....
答案 0 :(得分:4)
你的ondeviceready应该是第一个被调用的东西,而不是.ready。
看起来应该是这样的:
<script>
document.addEventListener("deviceready", deviceIsReady, false);
function deviceIsReady() {
/*This is where all of you initialization code should go.
With PhoneGap the deviceready should be the first thing*/
}
</script>
坚持使用PhoneGap的设备而不是$(文件).ready()