I am working with Cordova and Ionic and trying to get the device uuid using the Cordova plugin and ngCordova for device (http://ngcordova.com/docs/plugins/device/).
Problem
$cordovaDevice.getDevice() throws a device undefined error when running in the IonicView app installed on IPhone IOS 9.2.1 when called from $ionicPlatform.ready. However, it works fine when called from deviceready event.
Interestingly, $ionicPlatform.ready works fine on Android device or Windows 8.1 device.
Here is some code to recreate the problem:
angular.module('starter', ['ionic', 'ngCordova', 'starter.controllers', 'starter.services'])
.run(function ($ionicPlatform, $cordovaDevice) {
document.addEventListener('deviceready', onDeviceReady.bind(this), false);
function onDeviceReady() {
try {
var device = $cordovaDevice.getDevice();
alert("onDeviceReady Success!");
} catch (e) {
console.warn("onDeviceReady error: " + e)
alert("onDeviceReady error: " + e);
}
};
$ionicPlatform.ready(function () {
try {
var device = $cordovaDevice.getDevice();
alert("ionicPlatform.ready Success!")
} catch (e) {
console.warn("ionicPlatform.ready error: " + e)
alert("ionicPlatform.ready error: " + e);
}
});
})
This code will produce 2 alerts with the following messages:
- "ionicPlatform.ready error: ReferenceError: Can't find variable: device"
- "onDeviceReady Success!"
I really would expect that ionicPlatform.ready would not fire until the deviceready event fired.
Any thoughts why this is happening or how to guarantee the device has been loaded before calling $cordovaDevice.getDevice();
Thanks, Tom