Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions motion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ npx cap sync

## Permissions

This plugin is currently implemented using Web APIs. Most browsers require
permission before using this API. To request permission, prompt the user for
permission on any user-initiated action (such as a button click):
This plugin is currently implemented using Web APIs. On iOS devices,
permission must be requested before accessing device motion or orientation
events. To request permission, prompt the user on any user-initiated action
(such as a button click):

```typescript
import { PluginListenerHandle } from '@capacitor/core';
import { Motion } from '@capacitor/motion';


let accelHandler: PluginListenerHandle;

myButton.addEventListener('click', async () => {
try {
await DeviceMotionEvent.requestPermission();
} catch (e) {
// Handle error
return;
let orientationHandler: PluginListenerHandle;

myAccelerationButton.addEventListener('click', async () => {
if (typeof DeviceMotionEvent.requestPermission === 'function') {
try {
const permission = await DeviceMotionEvent.requestPermission();
if (permission !== 'granted') return;
} catch (e) {
// Handle error
return;
}
}

// Once the user approves, can start listening:
Expand All @@ -36,13 +41,37 @@ myButton.addEventListener('click', async () => {
});
});

myOrientationButton.addEventListener('click', async () => {
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
try {
const permission = await DeviceOrientationEvent.requestPermission();
if (permission !== 'granted') return;
} catch (e) {
// Handle error
return;
}
}

// Once the user approves, can start listening:
orientationHandler = await Motion.addListener('orientation', event => {
console.log('Device orientation event:', event);
});
});

// Stop the acceleration listener
const stopAcceleration = () => {
if (accelHandler) {
accelHandler.remove();
}
};

// Stop the orientation listener
const stopOrientation = () => {
if (orientationHandler) {
orientationHandler.remove();
}
};

// Remove all listeners
const removeListeners = () => {
Motion.removeAllListeners();
Expand All @@ -51,7 +80,10 @@ const removeListeners = () => {

See the
[`DeviceMotionEvent`](https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent)
API to understand the data supplied in the 'accel' event.
and
[`DeviceOrientationEvent`](https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent)
APIs to understand the data supplied in the `'accel'` and `'orientation'`
events respectively.

## API

Expand Down
Loading