Skip to content

Commit d9fd7df

Browse files
fix examples
1 parent 9cc9915 commit d9fd7df

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

motion/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ npx cap sync
1111

1212
## Permissions
1313

14-
This plugin is currently implemented using Web APIs. Most browsers require
15-
permission before using these APIs. To request permission, prompt the user for
16-
permission on any user-initiated action (such as a button click):
14+
This plugin is currently implemented using Web APIs. On iOS, permission must
15+
be requested before accessing device motion or orientation events. Other
16+
platforms do not require the check. To request permission, prompt the user
17+
on any user-initiated action (such as a button click):
1718

1819
```typescript
1920
import { PluginListenerHandle } from '@capacitor/core';
@@ -24,11 +25,14 @@ let accelHandler: PluginListenerHandle;
2425
let orientationHandler: PluginListenerHandle;
2526

2627
myAccelerationButton.addEventListener('click', async () => {
27-
try {
28-
await DeviceMotionEvent.requestPermission();
29-
} catch (e) {
30-
// Handle error
31-
return;
28+
if (typeof DeviceMotionEvent.requestPermission === 'function') {
29+
try {
30+
const permission = await DeviceMotionEvent.requestPermission();
31+
if (permission !== 'granted') return;
32+
} catch (e) {
33+
// Handle error
34+
return;
35+
}
3236
}
3337

3438
// Once the user approves, can start listening:
@@ -38,11 +42,14 @@ myAccelerationButton.addEventListener('click', async () => {
3842
});
3943

4044
myOrientationButton.addEventListener('click', async () => {
41-
try {
42-
await DeviceOrientationEvent.requestPermission();
43-
} catch (e) {
44-
// Handle error
45-
return;
45+
if (typeof DeviceOrientationEvent.requestPermission === 'function') {
46+
try {
47+
const permission = await DeviceOrientationEvent.requestPermission();
48+
if (permission !== 'granted') return;
49+
} catch (e) {
50+
// Handle error
51+
return;
52+
}
4653
}
4754

4855
// Once the user approves, can start listening:

0 commit comments

Comments
 (0)