如何使用 cordova 插件获取 ionic 中的设备 ID?
How to get device id in ionic by using cordova plugin?
我正在开发 混合离子应用程序 。
我想获取 android 设备的设备 ID。我安装了 cordova 插件,包括 cordova.min.js 文件。
我试过这段代码,还是没有显示任何东西。
还有其他获取设备ID的方法吗?
angular.module('starter', ['ionic','ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('DeviceController', function($ionicPlatform, $scope, $cordovaDevice) {
$ionicPlatform.ready(function() {
$scope.$apply(function() {
var device = $cordovaDevice.getDevice();
$scope.manufacturer = device.manufacturer;
$scope.model = device.model;
$scope.platform = device.platform;
$scope.uuid = device.uuid;
});
});
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/ng-cordova.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter" ng-controller="DeviceController">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Device Information</h1>
</ion-header-bar>
<ion-content>
<div class="item item-text-wrap">
<ul class="list">
<li class="item">
Manufacturer : {{manufacturer}}
</li>
<li class="item">
Model : {{model}}
</li>
<li class="item">
Platform : {{platform}}
</li>
<li class="item">
UUID : {{uuid}}
</li>
</ul>
</div>
</ion-content>
</ion-pane>
</body>
</html>
如果您已经安装设备插件,只需window.device.uuid
(在您收到设备就绪事件后)。
编辑
再次查看您的代码,您不需要在控制器中准备好平台,我不确定它是否会像那样触发。还有你为什么在那里打电话申请?这个怎么样:
.controller('DeviceController', function () {
alert(window.device.uuid);
});
您可以在您的项目中添加以下插件UniqueDeviceID
并在 ondevice ready 中添加以下行
window.plugins.uniqueDeviceID.get(成功,失败);
在您的代码中添加成功和失败函数
function success(){
alert(uuid);
}
function fail(error){
alert("error " + error);
}
这里要注意的是 ng-cordova.js 内部调用方法和 API 的 od native cordova 因此即使在安装 ng- 之后单独安装 cordova 插件也非常重要cordova.js。然后我在 app.module.
中初始化了设备
$ionicPlatform.ready(function() {
$scope.deviceInformation = ionic.Platform.device();
});
我在介绍控制器中调用了该方法
$scope.getDeviceInfo = function() {
alert($scope.deviceInformation.uuid);
}
或者通过 http://ngcordova.com/docs/plugins/device/ 会帮助你..
我明白了,代码很完美,没有任何问题,甚至插件安装正确。问题是 ng-cordova.min.js。我刚刚下载了最新版本的 ng-cordova.min.js。它工作完美。
谢谢大家。
这可能会对其他人有所帮助。
我正在开发 混合离子应用程序 。
我想获取 android 设备的设备 ID。我安装了 cordova 插件,包括 cordova.min.js 文件。
我试过这段代码,还是没有显示任何东西。
还有其他获取设备ID的方法吗?
angular.module('starter', ['ionic','ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
})
.controller('DeviceController', function($ionicPlatform, $scope, $cordovaDevice) {
$ionicPlatform.ready(function() {
$scope.$apply(function() {
var device = $cordovaDevice.getDevice();
$scope.manufacturer = device.manufacturer;
$scope.model = device.model;
$scope.platform = device.platform;
$scope.uuid = device.uuid;
});
});
})
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/ng-cordova.min.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter" ng-controller="DeviceController">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Device Information</h1>
</ion-header-bar>
<ion-content>
<div class="item item-text-wrap">
<ul class="list">
<li class="item">
Manufacturer : {{manufacturer}}
</li>
<li class="item">
Model : {{model}}
</li>
<li class="item">
Platform : {{platform}}
</li>
<li class="item">
UUID : {{uuid}}
</li>
</ul>
</div>
</ion-content>
</ion-pane>
</body>
</html>
如果您已经安装设备插件,只需window.device.uuid
(在您收到设备就绪事件后)。
编辑
再次查看您的代码,您不需要在控制器中准备好平台,我不确定它是否会像那样触发。还有你为什么在那里打电话申请?这个怎么样:
.controller('DeviceController', function () {
alert(window.device.uuid);
});
您可以在您的项目中添加以下插件UniqueDeviceID
并在 ondevice ready 中添加以下行
window.plugins.uniqueDeviceID.get(成功,失败);
在您的代码中添加成功和失败函数
function success(){
alert(uuid);
}
function fail(error){
alert("error " + error);
}
这里要注意的是 ng-cordova.js 内部调用方法和 API 的 od native cordova 因此即使在安装 ng- 之后单独安装 cordova 插件也非常重要cordova.js。然后我在 app.module.
中初始化了设备$ionicPlatform.ready(function() {
$scope.deviceInformation = ionic.Platform.device();
});
我在介绍控制器中调用了该方法
$scope.getDeviceInfo = function() {
alert($scope.deviceInformation.uuid);
}
或者通过 http://ngcordova.com/docs/plugins/device/ 会帮助你..
我明白了,代码很完美,没有任何问题,甚至插件安装正确。问题是 ng-cordova.min.js。我刚刚下载了最新版本的 ng-cordova.min.js。它工作完美。 谢谢大家。 这可能会对其他人有所帮助。