简体   繁体   English

如何在ionic框架中获取设备UUID

[英]How to get the device UUID in ionic framework

installed cordova device plugin by :通过以下方式安装cordova设备插件:

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然后下载 ngCordova 并将 ng-cordova.min.js 包含在 js 文件夹中,也包含在 index.html 中

next what i did is injected ngCordova as follows接下来我所做的是注入 ngCordova 如下

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.如果有另一种读取设备 UUID 的方法,请告诉我它的方向。

Yes, there is another way.是的,还有另一种方式。 You just don't need the ngCordova for this.你只是不需要 ngCordova为此。

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 .当您添加插件cordova plugin add org.apache.cordova.device它会加载到您的应用程序中,因此您想要的信息位于window.device

If you want to get device uuid at anywhere in the code you just need to call window.device.uuid .如果你想在代码的任何地方获取设备 uuid,你只需要调用window.device.uuid

If you want as soon as the app starts, then use:如果您想在应用程序启动后立即使用,请使用:

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

If you are using '> ionic serve', device will be "not defined."如果您使用“> ionic serve”,则设备将“未定义”。 Try in an emulator or physical device.在模拟器或物理设备中尝试。

Use ngCordova and cordova Device plugin :使用ngCordova 和 cordova 设备插件

cordova plugin add org.apache.cordova.device cordova 插件添加 org.apache.cordova.device

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

Within v2 it works like this:在 v2 中,它的工作方式如下:

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

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

You could just use ionic.Platform.device() in your platform.ready function.您可以在platform.ready函数中使用ionic.Platform.device()

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

hope this helps someone :).希望这有助于某人:)。

Regards.问候。

Install:安装:

@ionic-native/core @ionic-native/device @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';"如果不运行更改"import { Device } from '@ionic-native/device';" for "import { Device } from '@ionic-native/device/ngx';"对于"import { Device } from '@ionic-native/device/ngx';"

And "this.device.uuid" for "Investigate" “调查”的"this.device.uuid"

Use these commands for run in browser使用这些命令在浏览器中运行

    ionic build

    ionic cordova platform add browser

    cordova run browser

And works !和作品! in these versions在这些版本中

在此处输入图片说明

in Browser在浏览器中

在此处输入图片说明

in Real Device在真实设备中

在此处输入图片说明

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

You may only access cordova plugins within the ionic.Platform.ready() callback function:您只能在 ionic.Platform.ready() 回调函数中访问cordova插件:

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.这是因为 Cordova 插件比 Web 应用程序需要更多的时间来加载。 The ionic.Platform.ready() callback is triggered as soon Cordova has fully loaded or immediately if it is already loaded. ionic.Platform.ready() 回调在 Cordova 完全加载后立即触发,如果已经加载则立即触发。

Have been struggling with this for hours today, install the cordova device plugin with:今天已经为此苦苦挣扎了几个小时,安装cordova设备插件:

cordova plugin add cordova-plugin-device

make sure you also reference the plugin in your config.xml:确保您还在 config.xml 中引用了该插件:

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

wow found out what wrong i was doing... through this question.哇发现我做错了什么......通过这个问题。 http://forum.ionicframework.com/t/problem-to-use-ngcordova-device-is-not-defined/11979 http://forum.ionicframework.com/t/problem-to-use-ngcordova-device-is-not-defined/11979

when we test on other device which has other platform than cordova supports this happens.当我们在具有其他平台而不是cordova支持的其他设备上进行测试时,会发生这种情况。 this was a useful discovery for me.这对我来说是一个有用的发现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM