iOS设备上的Cordova应用程序无法启动

我有index.html我有deviceready事件侦听器添加到脚本标记。 但是在加载HTML时不会触发。 相反,当点击主页button时,它将从onAppDidEnterBackground方法触发。

我想调用我的自定义插件来获取我正试图填充的HTML中加载的值。 我发现很less有解决scheme要求更改meta标签。 我曾尝试改变,但没有用。 它也不适用于iOS9。 我想元标记问题是从iOS10。 我不知道我在这里错过了什么。

Cordova v4.4.0

的index.html

 <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: http://* 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;"> <meta name="msapplication-tap-highlight" content="no"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"> <title>My HTML Document</title> <link rel="stylesheet" type="text/css" href="style.css" /> <script type="text/javascript" src='cordova.js'></script> <script type="text/javascript" src='CustomPlugin.js'></script> <script> document.addEventListener('deviceready', function () { window.plugins.CustomPlugin.myMethod(function (result) { document.getElementById('Name').value = result['Name']; }, function (error) { alert(error); }); }, false); </script> </head> <body> <div class='article'> <div class='tableWrapper'> <table> <tr> <td class='right'>CName:</td> <td colspan='3'><input type='text' id='Name'/></td> </tr> </table> </div> </div> </body> </html> 

尝试把你的addEventListener放入一个函数onLoad中,并在你的body标签中用onload事件调用它:

  function onLoad() { document.addEventListener("deviceready", onDeviceReady, false); } function onDeviceReady(){ //the code you want to execute } 

 <body onload="onLoad()"> 

因为如果按照自己的方式进行操作,则会在装入cordova库之前尝试执行deviceready事件。

将所有脚本加载到html正文的底部

 <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> 

并创buildmain.js文件,然后把你的代码在那里..

 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);