Deviceready Not Firing In Cordova App On Ios
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 f
Solution 1:
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.
<metahttp-equiv="Content-Security-Policy"content="default-src 'self' data:* gap://* tel:* 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'" />
Solution 2:
Try to put your addEventListener into a function onLoad and call it with onload event in your body tag :
functiononLoad() {
document.addEventListener("deviceready", onDeviceReady, false);
}
functiononDeviceReady(){
//the code you want to execute
}
and
<bodyonload="onLoad()">
Because if you do it your way, you're trying to do the deviceready event before the cordova library had been loaded.
Solution 3:
Load all your script on the bottom of html body
<htmllang="en"><head>...</head><body><div>Your content here</div>
...
<scripttype="text/javascript"src='CustomPlugin.js'></script><scripttype="text/javascript"src='cordova.js'></script><scripttype="text/javascript"src='main.js'></script></body></html>
and create main.js file then put your code in there..
window.onload = function(){
document.addEventListener("deviceready", firstInitCordova, true);
};
functionfirstInitCordova(){
if(!window.cordova){
// If cordova is not defined then call this function again after 2 secondsetTimeout(firstInitCordova, 2000);
return;
}
//console.log(window.plugins);window.plugins.CustomPlugin.myMethod(function (result) {
document.getElementById('Name').value = result['Name'];
}, function (error) {
alert(error);
});
}
// If you're unsure then set a timersetTimeout(function(){
firstInitCordova();
}, 3000);
Post a Comment for "Deviceready Not Firing In Cordova App On Ios"