Reputation: 864
I know this is a common issue but all the workarounds found were "include cordova.js in index.html" or "change href="." to href="./""
I think cordova API is not loaded in the application because deviceready callback is not called. But I can still run the app (but components using navigagor.mediadevices doesn't work).
index.html
<base href="./">
<script src=”cordova.js”></script>
<script>
window.isready = false;
function ready(){
alert('test');
console.log('test');
window.isready = true; // this is always false
}
document.addEventListener('deviceready', ready, false);
</script>
The event is never fired
How i installed cordova & build the app
npm install -g cordova
cordova create cordova
cd cordova && cordova platforms add android
rm -rf cordova/www/*
ng build --prod --aot --output-path=cordova/www
cd cordova && cordova build android
cordova's package.json
...
"dependencies": {
"cordova-android": "^7.0.0",
"cordova-plugin-device": "^2.0.2",
"cordova-plugin-file": "^6.0.1",
"cordova-plugin-media-capture": "^3.0.2",
"cordova-plugin-whitelist": "^1.3.3"
},
"cordova": {
"plugins": {
"cordova-plugin-whitelist": {},
"cordova-plugin-media-capture": {},
"cordova-plugin-device": {}
},
"platforms": [
"android"
]
}
Upvotes: 1
Views: 1455
Reputation: 17661
The setup for your event handler of deviceready
is correct. The problem appears to be your inclusion of cordova.js
. Here's what you should do to troubleshoot:
cordova.js
using slanted double quotes, instead of plain double quotes.Try changing this:
<script src=”cordova.js”></script>
To this:
<script src="cordova.js"></script>
Run the app on your Android device, connect your device to your computer via USB, enabling developer mode debugging on your device, and open up chrome://inspect
to live inspect the app as it's running. Make sure that cordova.js
actually exists and that it was successfully loaded upon app boot.
The order in which you include cordova.js
can make a difference. You may need to move <script src="cordova.js"></script>
beneath the <script>
block where you setup the deviceready
event handler.
Upvotes: 8