IBM Worklight 6.0 - Cordova onDeviceReady is not firing
-
21-12-2019 - |
Question
I'm using worklight 6.0.
The deviceready event is not firing. I'm testing this in the MBS, in iOS and Android both in Firefox and Chrome.
Here is the code I have used in initoptions.js
file:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
alert("device Ready")
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
alert is never getting fired.
I think worklight 6.0 using cordova 2.6 version, Do I need to change the cordova.js of any other version?
Solution
This check is not needed in a Worklight-based application.
As you correctly mentioned, Cordova is bundled with Worklight; Once your app successfully launches, the deviceReady
has already fired (internally). Otherwise, your app would not work to begin with.
App launch > Cordova initialization > Worklight framework initialization > App is "ready"
So what you need to do is to simply place your implementation code in common\yourApp.js
(or wherever you want and then call it), w/out the deviceReady
check.
For example, you'll note that if doing the following, it will work even w/out the check for devicReady
first.
function wlCommonInit() {
alert (device.cordova);
}
If to use the fileSystem example, which is what you are trying to do... I have done the following and it is working OK for me. I simply followed the LocalFileSystem example from the Cordova File API page.
- New project and application
Added the Android environment
The File API is meant for mobile environments and not for previewing in your PC browser.
This is the reason for the error you see:LocalFileSystem is not defined
. It has got nothing to do withdeviceReady
.In
common\testapp.js
I have added the following- Ran the app in Android Emulator
In the app, I got the success alert()
function wlCommonInit(){ window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); } function onFileSystemSuccess(fileSystem) { alert("*** Success *** File system name is: " + fileSystem.name); } function fail(evt) { alert("*** Fail *** Error code is: " + evt.target.error.code); }
To do the same when previewing the Android environment via Worklight Console's MBS, you need to make sure you have Java installed and that it is active in your browser (to view Java applets). You should then use the File menu on the left pane.
See these questions:
- IBM Worklight - File API simulation in Worklight Console's MBS
- IBM Worklight 6.0 - "Capture API simulation is disabled (applet not loaded, please check your Java settings)"
If you are on Mac, you need to use Java 6.