问题描述
我有一个简单的Ionic页面,应点击Capacitor的Motion插件,并显示设备指南针的方向.
I have a simple Ionic page that should tap into the Motion plugin of Capacitor and show the orientation of the compass the device.
可以在Chrome开发者控制台中正常运行,但不能在设备本身上运行-是否通过XCode访问ionic服务器.
Works fine in the Chrome dev console but not on the device itself - whether accessing the ionic server og via XCode.
代码在这里:
import {Component} from '@angular/core';
import {MotionOrientationEventResult, PluginListenerHandle, Capacitor} from '@capacitor/core';
const {Motion} = Capacitor.Plugins;
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
private orientation: MotionOrientationEventResult;
private watchListener: PluginListenerHandle;
private entered = null;
private alpha = 0;
constructor() {
this.watchListener = Motion.addListener(
'orientation', (values) => {
console.log('watchListener', values);
this.orientation = values;
this.alpha = values.alpha;
});
}
}
还有package.json在这里:
And package.json here:
{
"name": "compass",
"version": "0.0.1",
"author": "Ionic Framework",
"homepage": "https://ionicframework.com/",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"ionic-serve-c": "ionic serve --devapp --host=192.168.68.101 --ssl",
"open-ios": "npx cap open ios"
},
"private": true,
"dependencies": {
"@angular/common": "~10.0.0",
"@angular/core": "~10.0.0",
"@angular/forms": "~10.0.0",
"@angular/platform-browser": "~10.0.0",
"@angular/platform-browser-dynamic": "~10.0.0",
"@angular/router": "~10.0.0",
"@capacitor/core": "^2.4.1",
"@capacitor/ios": "^2.4.1",
"@ionic-native/core": "^5.0.0",
"@ionic-native/splash-screen": "^5.0.0",
"@ionic-native/status-bar": "^5.0.0",
"@ionic/angular": "^5.0.0",
"cordova-plugin-device-motion": "^2.0.1",
"rxjs": "~6.5.5",
"tslib": "^2.0.0",
"zone.js": "~0.10.3"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1000.0",
"@angular/cli": "~10.0.5",
"@angular/compiler": "~10.0.0",
"@angular/compiler-cli": "~10.0.0",
"@angular/language-service": "~10.0.0",
"@capacitor/cli": "^2.4.1",
"@ionic/angular-toolkit": "^2.3.0",
"@types/jasmine": "~3.5.0",
"@types/jasminewd2": "~2.0.3",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.5.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage-istanbul-reporter": "~3.0.2",
"karma-jasmine": "~3.3.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~3.9.5"
},
"description": "An Ionic project"
}
可以在此处看到整个项目: https://github.com/bsnizek/compass
The whole project can be seen here: https://github.com/bsnizek/compass
感谢!
推荐答案
在iOS 13或更高版本上,您需要先使用 DeviceMotionEvent.requestPermission();
请求权限,然后再使用运动
On iOS 13+ you need to request a permission with DeviceMotionEvent.requestPermission();
before using motion
在文档> https://capacitorjs.com/docs/apis/motion#权限
这篇关于离子+电容器:Motion在设备上不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!