How to get the device UUID in ionic framework

2019-02-04 13:34发布

installed cordova device plugin by :

sudo cordova plugin add org.apache.cordova.device

then downloaded ngCordova and included ng-cordova.min.js in to js folder and also included in index.html

next what i did is injected ngCordova as follows

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

then included in controller as follows

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)

Can you now tell me what went wrong?

If is there another way to read the Device UUID show me the direction to it.

9条回答
欢心
2楼-- · 2019-02-04 13:54

You could just use ionic.Platform.device() in your platform.ready function.

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

hope this helps someone :).

Regards.

查看更多
女痞
3楼-- · 2019-02-04 13:55

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

You may only access cordova plugins within the ionic.Platform.ready() callback function:

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

})

This is because Cordova plugins take some more time to load then the web application. The ionic.Platform.ready() callback is triggered as soon Cordova has fully loaded or immediately if it is already loaded.

查看更多
Emotional °昔
4楼-- · 2019-02-04 14:00

Yes, there is another way. You just don't need the ngCordova for this.

When you add the plugin cordova plugin add org.apache.cordova.device it's loaded to your application and therefore the info you want is at window.device.

If you want to get device uuid at anywhere in the code you just need to call window.device.uuid.

If you want as soon as the app starts, then use:

ionic.Platform.ready(function(){
  console.log( window.device.uuid );
});
查看更多
何必那么认真
5楼-- · 2019-02-04 14:03

If you are using '> ionic serve', device will be "not defined." Try in an emulator or physical device.

查看更多
走好不送
6楼-- · 2019-02-04 14:11

Within v2 it works like this:

import { Device } from 'ionic-native';
console.log('Device UUID is: ' + Device.uuid);

Reference: http://ionicframework.com/docs/v2/native/device/

查看更多
狗以群分
7楼-- · 2019-02-04 14:11

Install:

@ionic-native/core @ionic-native/device

enter link description here

ionic cordova plugin add cordova-plugin-device
npm install --save @ionic-native/device

Add this plugin to your app's module

 // app.module.ts
import { Device } from '@ionic-native/device';

...

@NgModule({
  ...

  providers: [
    ...
    Device
    ...
  ]
  ...
})
export class AppModule { }

Usage

import { Device } from '@ionic-native/device';

constructor(private device: Device) { }

...

console.log('Device Model is: ' + this.device.model+
  '\n Device UUID is: ' + this.device.uuid+
  '\n Device serial is: ' + this.device.serial+
  '\n Device platform is: ' + this.device.platform+
  '\n Device version is: ' + this.device.version+
  '\n Device manufacturer is: ' + this.device.manufacturer);

If won't run change "import { Device } from '@ionic-native/device';" for "import { Device } from '@ionic-native/device/ngx';"

And "this.device.uuid" for "Investigate"

Use these commands for run in browser

    ionic build

    ionic cordova platform add browser

    cordova run browser

And works ! in these versions

enter image description here

in Browser

enter image description here

in Real Device

enter image description here

查看更多
登录 后发表回答