2016-01-22 76 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()之前被加載。

感謝, 湯姆

回答