2016-01-22 74 views
1

我正在与Cordova和Ionic一起尝试使用Cordova插件和ngCordova设备(http://ngcordova.com/docs/plugins/device/)获取设备uuid。

问题

$ cordovaDevice.getDevice()在安装在IPhone IOS 9.2.1从$ ionicPlatform.ready调用时IonicView应用运行时将引发装置未定义的错误。但是,从deviceready事件中调用时它工作正常。

有趣的是,$ ionicPlatform.ready适用于Android设备或Windows 8.1设备。

下面是一些代码来重现问题:

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); 
    } 
}); 
}) 

该代码将产生2个警报与以下消息:

  • “ionicPlatform.ready错误:的ReferenceError:找不到变量: 设备“
  • ”onDeviceReady Success!“

我真的希望ionicPlatform.ready在deviceready事件触发前不会触发。

任何想法为什么会发生这种情况,或者如何保证设备在调用$ cordovaDevice.getDevice()之前被加载。

感谢, 汤姆

回答