问题
I've been banging my head on this one for a few days. After getting a number of "object not defined" errors when trying to create a new FileTransfer() object, it looks like the problem is more basic -- somehow the DeviceReady event is not firing.
Stack Overflow has a lot of hits on this issue, but most of them have to do with pre-3.x cordova builds that had a different architecture (I'm on 4.1.2). I've tried the suggestions in the newer topics I could find -- removing and adding plugins, updating cordova, etc. -- to no avail. To try to isolate the issue, I've commented out the startup code to just a few lines:
Index.html:
<!DOCTYPE html>
<html>
<head>
<title>Blah</title>
<meta charset="utf-8"/>
<meta name="viewport" content="initial-scale=1, user-scalable=no, minimum-scale=1, maximum-scale=1">
<link href="res/topcoat/css/topcoat-mobile-light.min.css" rel="stylesheet">
<link href="res/css/styles.css" rel="stylesheet">
<link href="res/css/pageslider.css" rel="stylesheet">
<script data-main="js/main" src="lib/require.js"></script>
</head>
Main.js:
require(["app/Application"], function (Application) {
"use strict";
document.addEventListener("deviceready", function(){
$('body').html("<p>device is ready</p>");
},true);
$('body').html("<p>waiting...</p>");
});
Instead of displaying "device is ready" in the body after a bit, the screen just displays "waiting...". This happens on both the iOS emulator and the browser (cordova emulate browser).
Cordova info:
$ cordova -v
4.1.2
Plugin info:
$ cordova plugins
org.apache.cordova.globalization 0.3.3 "Globalization"
(I get the same results if Globalization isn't there).
Is there some other place I should be looking? I'm running from the command line, if that makes a difference.
回答1:
I think in this case you need to include cordova.js
in your application, because I don't see cordova.js
in your example
<script src="cordova.js"></script>
Note: path to cordova.js
depends on where it located in your app
回答2:
That did not initially fix it for me until I removed
< meta http-equiv="Content-Security-Policy" content=".." / >
回答3:
I recently had this same issue, but in my case cordova.js
was already properly included.
Eventually what worked for me was a simple remove
and add
of the ios
platform:
cordova platform remove ios
cordova platform add ios
It had been quite a while since I had completely re-built the ios
platform and other major changes had taken place during that time (Cordova upgrade, XCode upgrade, etc). It's possible that my config.xml
or existing ios
build was somehow incompliant with the latest Cordova requirements.
回答4:
I have been trying to fix this issue for DAYS and I finally got deviceready
to trigger. The issue was that I had extended the js Object
to insert my own hide and show commands. Removing those lines allowed deviceready
to trigger:
Object.prototype.hide = function(){
this.style.display = 'none';
}
Object.prototype.show = function(){
this.style.display = 'initial';
}
note: I also had to have the line <script src="cordova.js"></script>
as mentioned by Alexander T
来源:https://stackoverflow.com/questions/27865265/cordova-deviceready-not-firing