简体   繁体   English

Phonegap设备已经在iOS中使用Cordova 2.2.0无法启动

[英]Phonegap deviceready not firing using Cordova 2.2.0 in iOS

I am building a PhoneGap App. 我正在构建一个PhoneGap应用程序。 Unfortunately, when deploying to iOS devices and simulators the deviceready event never fires. 不幸的是,当部署到iOS设备和模拟器时, deviceready事件永远不会触发。 I'm using Phonegap 2.2.0. 我正在使用Phonegap 2.2.0。

When I deploy the same code to Android (using the Android-specific cordova.js file of course) the App will work perfectly. 当我将相同的代码部署到Android(当然使用Android特定的cordova.js文件)时,应用程序将完美运行。

When I replace the deviceready with a jQuery- ready() the app will load on iOS as well, yet it will then lack access to the device specific APIs. 当我用deviceready - ready()替换deviceready ,应用程序也将加载到iOS上,但它将无法访问特定于设备的API。

The cordova.js is loaded as I will see a simple alert message that I put inside of it, yet deviceready never fires and the APIs are never exposed. 加载了cordova.js ,因为我将看到一条简单的警报消息,我将其置于其中,但是deviceready从未触发,并且API永远不会暴露。

My HTMLs head : 我的HTML head

<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script> <!-- yes it is the iOS version -->
<script src="js/jquery-1.8.2.min.js"></script>
<script src="js/app.js"></script>

My JS: 我的JS:

function doStuff(){
//app functionality
}
document.addEventListener('deviceready', doStuff, false);

But somehow stuff will only get done on Android... 但不知何故,东西只会在Android上完成......

in my html I have a onload that triggers that adding of an event listener to deviceready 在我的html中,我有一个onload,触发向deviceready添加事件监听器

      function onDeviceReady() {
        console.log("we are an app");
        MyApp.initialize_phonegap();
      }

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

    </script>

  </head>

  <body onload="onBodyLoad()">

To add to olore's answer I ended up using the approach that the code in the default project (that gets built from the ./create script) uses (which is different to the code in the Event docs ). 为了添加到olore的答案,我最终使用了默认项目中的代码(从./create脚本构建)使用的方法(这与Event文档中的代码不同)。

The major differences are (I do not really know which one of these actually are to be taken into account): 主要区别是(我真的不知道其中哪一个实际上要考虑到):

  • cordova-2.2.0.js is located in the root folder cordova-2.2.0.js位于根文件夹中
  • <script> s are included right before the closing </body> -tag and not in the document's head <script>包含在结束</body> -tag之前,而不是文档的head
  • deviceready -handling works like: deviceready工作原理如下:

     var app = { // Application Constructor initialize: function() { this.bindEvents(); }, // Bind Event Listeners // // Bind any events that are required on startup. Common events are: // 'load', 'deviceready', 'offline', and 'online'. bindEvents: function() { document.addEventListener('deviceready', this.onDeviceReady, false); }, // deviceready Event Handler // // The scope of 'this' is the event. In order to call the 'receivedEvent' // function, we must explicity call 'app.receivedEvent(...);' onDeviceReady: function() { app.receivedEvent('deviceready'); myApp.start(); //this is where I put the call to my App's functionality relying on device APIs }, // Update DOM on a Received Event receivedEvent: function(id) { // I didn't really use this, yet I left it in here as it's in the demo 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); } }; 
  • The last <script> tag just calls app.initialize() 最后一个<script>标记只调用app.initialize()

This seems to work quite fine on iOS and Android and is a little more understandable to me than the double handler nesting from the docs. 这似乎在iOS和Android上运行得相当好,对我来说比从文档嵌套的双处理程序更容易理解。

It seems to make a difference if you are adding the deviceready listener before or after the cordova.js : 这似乎有所作为,如果你之前之后 cordova.js加入deviceready监听器:

I was not able to find any documentation on this, but cordova.js intercepts calls to addEventListener + removeEventListener and calls only deviceready callbacks which has been added before cordova.js. 我无法找到任何关于此的文档,但是cordova.js拦截了对addEventListener + removeEventListener的调用,并且只调用了 cordova.js 之前添加的deviceready回调。

Solution in my case was just to rearrange the script order: 我的解决方案只是重新排列脚本顺序:

<script>
document.addEventListener('deviceready', ...)
</script>
<script src="cordova.js"></script>

我发现如果你不小心包含了两次cordova.js脚本,那么deviceready事件就不会触发。

I was having the same issue. 我遇到了同样的问题。 I got it to work by adding the device plugin. 我通过添加设备插件让它工作。

$ cordova plugin add org.apache.cordova.device

To verify: 核实:

$ cordova plugin ls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM