2014-12-14 5 views
14

installiert cordova Gerät Plugin erhalten von:Wie das Gerät UUID in ionischer Rahmen

sudo cordova plugin add org.apache.cordova.device 

dann ngCordova heruntergeladen und enthalten ng-cordova.min.js um js Ordner und auch in index.html

enthalten

nächste, was ich tat wird injiziert ngCordova als

angular.module('starter', ['ionic', 'starter.controllers','ngCordova']) 

dann in der Steuerung folgt enthalten als

folgt
angular.module('starter.controllers', []) 

.controller('AppCtrl', function($scope, $ionicModal, $timeout, $ionicPlatform,$cordovaDevice) 
but still getting the following errors 

ReferenceError: device is not defined 
at Object.getUUID (http://localhost:8100/js/ng-cordova.min.js:1:14929) 
at new <anonymous> (http://localhost:8100/js/controllers.js:27:26) 
at invoke (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11591:17) 
at Object.instantiate (http://localhost:8100/lib/ionic/js/ionic.bundle.js:11602:23) 
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:14906:28 
at updateView (http://localhost:8100/lib/ionic/js/ionic.bundle.js:42986:30) 
at eventHook (http://localhost:8100/lib/ionic/js/ionic.bundle.js:42933:17) 
at Scope.$broadcast (http://localhost:8100/lib/ionic/js/ionic.bundle.js:20605:28) 
at $state.transition.resolved.then.$state.transition (http://localhost:8100/lib/ionic/js/ionic.bundle.js:34122:22) 
at wrappedCallback (http://localhost:8100/lib/ionic/js/ionic.bundle.js:19197:81) 

Können Sie mir jetzt sagen, was schief gelaufen ist?

Wenn es eine andere Möglichkeit gibt, die Device UUID zu lesen, zeige mir die Richtung dazu.

Antwort

8

Verwendung ngCordova and cordova Device plugin:

cordova Plugin org.apache.cordova.device

module.controller('MyCtrl', function($scope, $cordovaDevice) { 
    var uuid = $cordovaDevice.getUUID(); 
}); 
9

Wenn Sie '> ionic serve' verwenden, wird das Gerät "nicht definiert". Probieren Sie einen Emulator oder ein physikalisches Gerät aus.

+1

guter Punkt - als dies wird wahrscheinlich nicht sehr offensichtlich für viele Leute (einschließlich mir selbst) – Zabs

1

http://forum.ionicframework.com/t/ionic-cordova-device-uuid/13652

Sie dürfen nur Zugang cordova Plugins innerhalb des ionic.Platform.ready() Callback-Funktion:

angular.module('starter.controllers', []) 

.controller('DashCtrl', function ($scope, $state, $cordovaDevice) { 

var init = function() { 
    console.log("initializing device"); 
    try { 

    $scope.uuid = $cordovaDevice.getUUID(); 

    } 
    catch (err) { 
    console.log("Error " + err.message); 
    alert("error " + err.$$failure.message); 
    } 

}; 

ionic.Platform.ready(function(){ 
    init(); 
}); 

}) 

Dies liegt daran, Cordova Plugins etwas mehr Zeit in Anspruch nehmen dann die Web-Anwendung zu laden. Der ionic.Platform.ready() - Callback wird ausgelöst, sobald Cordova vollständig geladen ist oder sofort, wenn es bereits geladen ist.

13

Ja, es gibt einen anderen Weg. Sie brauchen dazu einfach nicht ngCordova.

Wenn Sie das Plugin cordova plugin add org.apache.cordova.device hinzufügen, wird es in Ihre Anwendung geladen und die gewünschte Information lautet daher window.device.

Wenn Sie Gerät Uuid irgendwo im Code erhalten möchten, müssen Sie nur anrufen window.device.uuid.

Wenn Sie so schnell wollen, wie die App gestartet wird, dann verwenden:

ionic.Platform.ready(function(){ 
    console.log(window.device.uuid); 
}); 
1

Wurden heute stundenlang kämpfen mit diesem, installieren Sie das cordova Gerät Plugin mit:

cordova plugin add cordova-plugin-device

make sicher, dass Sie auch das Plugin in Ihrer Konfiguration referenzieren.xml:

<plugin name="cordova-plugin-device" source="npm" spec="~1.1.1" />

2

Sie nur ionic.Platform.device() in Ihrer platform.ready Funktion nutzen könnten.

$ionicPlatform.ready(function { 
    console.log(ionic.Platform.device());// returns an object containing device uuid,version, platform, manufacturer ... 
}); 

hoffe das hilft jemandem :).

Grüße.