1

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

Thomas
  • 3,532
  • 3
  • 20
  • 22

1 Answers1

0

Maybe you can solve your issue adopting the solution to bootstrap Angular (and consequently Ionic) after device ready event fired.

There are some posts here on StackOverflow (but also on other forums) about this. Check for example:

How to Deviceready in right way in Ionic application?

Cordova + Angularjs + Device Ready

Community
  • 1
  • 1
beaver
  • 17,333
  • 2
  • 40
  • 66