离子+电容器:Motion在设备上不起作用

时间:2020-09-23 08:14:41

标签: typescript ionic-framework orientation capacitor motion

我有一个简单的Ionic页面,应点击Capacitor的Motion插件并显示设备指南针的方向。

在Chrome开发者控制台中工作正常,但不能在设备本身上正常工作-是否通过XCode访问ionic服务器。

代码在这里:

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在这里:

{
  "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

感谢!

2 个答案:

答案 0 :(得分:0)

在iOS 13+上,您需要在使用Motion之前向{{1}}请求权限

文档https://capacitorjs.com/docs/apis/motion#permissions

中对此进行了解释

答案 1 :(得分:-1)

只是一个想法,但插件使用的“必需的设备功能”下的info.plist中可能缺少功能。 https://developer.apple.com/documentation/bundleresources/information_property_list/uirequireddevicecapabilities也许是:加速度计或陀螺仪。