Skip to content Skip to sidebar Skip to footer

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.

<meta http-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 :

    function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
    }

    function onDeviceReady(){
           //the code you want to execute
    }

and

<body onload="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

<html lang="en">
<head>...</head>
<body>
  <div>Your content here</div>
  ...
  <script type="text/javascript" src='CustomPlugin.js'></script>
  <script type="text/javascript" src='cordova.js'></script>
  <script type="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);
};

function firstInitCordova(){
    if(!window.cordova){
        // If cordova is not defined then call this function again after 2 second
        setTimeout(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 timer
setTimeout(function(){
    firstInitCordova();
}, 3000);

Post a Comment for "Deviceready Not Firing In Cordova App On IOS"