[英]Vibration.vibrate works on android but the same code does not work on IOS
[英]ionic getting device information for ios does not work while same code works for Android
我试图找到我的代码在什么类型的 ios 设备上运行(iPhone 4s 或 iPhone 5 等)。
这是我拥有的 html 和 js 代码。 这是index.html :
<!DOCTYPE html>
<html>
<head>
<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">
<script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="js/ng-cordova.min.js"></script>
<script src="cordova.js"></script>
<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>
这是我的app.js :
angular.module('starter', ['ionic', 'ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
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;
});
});
});
该代码在 Android 上运行良好,并获取所有请求的信息,包括设备类型和 Android 版本等。但在 iPhone 上,该信息为空,我没有使用相同的代码获取任何信息。
我需要为 ios 更改什么吗?
我认为这样做的更好且有效的方法是:
.controller('DeviceController', function($ionicPlatform, $scope) {
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
$scope.manufacturer = device.manufacturer;
$scope.model = device.model;
$scope.platform = device.platform;
$scope.uuid = device.uuid;
}
});
您不需要定义变量var device = $cordovaDevice.getDevice();
您也可以使用您使用$ionicPlatform.ready()
的方式,文档侦听器只是我喜欢编写和查看它的一种方式,但我想您不需要 $scope.$apply() 任何东西。
官方文档: https : //github.com/apache/cordova-plugin-device
编辑(添加我按要求工作的个人代码)
不要介意我的模块结构和你的有点不同。
所以这是我的testController.js
angular.module('testApp.testController', [])
.controller('testController', function($scope) {
'use strict';
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
console.log('device ready');
$scope.manufacturer = device.manufacturer;
$scope.model = device.model;
$scope.platform = device.platform;
$scope.uuid = device.uuid;
}
});
这是我的test.html
<ion-view class="testview">
<ion-content class="test-list">
<div>Manufacturer: {{manufacturer}}</div>
<div>Model: {{model}}</div>
<div>Platform: {{platform}}</div>
<div>UUID: {{uuid}}</div>
</ion-content>
</ion-view>
然后我只运行ionic build ios
并使用 xcode 在我的设备上运行它。 它显示所有信息就好了。 也在 Android 中进行了测试,也可以正常工作!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.