@@ -11,23 +11,28 @@ npx cap sync
1111
1212## Permissions
1313
14- This plugin is currently implemented using Web APIs. Most browsers require
15- permission before using this API. 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 devices,
15+ permission must be requested before accessing device motion or orientation
16+ events. To request permission, prompt the user on any user-initiated action
17+ (such as a button click):
1718
1819``` typescript
1920import { PluginListenerHandle } from ' @capacitor/core' ;
2021import { Motion } from ' @capacitor/motion' ;
2122
2223
2324let accelHandler: PluginListenerHandle ;
24-
25- myButton .addEventListener (' click' , async () => {
26- try {
27- await DeviceMotionEvent .requestPermission ();
28- } catch (e ) {
29- // Handle error
30- return ;
25+ let orientationHandler: PluginListenerHandle ;
26+
27+ myAccelerationButton .addEventListener (' click' , async () => {
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+ }
3136 }
3237
3338 // Once the user approves, can start listening:
@@ -36,13 +41,37 @@ myButton.addEventListener('click', async () => {
3641 });
3742});
3843
44+ myOrientationButton .addEventListener (' click' , async () => {
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+ }
53+ }
54+
55+ // Once the user approves, can start listening:
56+ orientationHandler = await Motion .addListener (' orientation' , event => {
57+ console .log (' Device orientation event:' , event );
58+ });
59+ });
60+
3961// Stop the acceleration listener
4062const stopAcceleration = () => {
4163 if (accelHandler ) {
4264 accelHandler .remove ();
4365 }
4466};
4567
68+ // Stop the orientation listener
69+ const stopOrientation = () => {
70+ if (orientationHandler ) {
71+ orientationHandler .remove ();
72+ }
73+ };
74+
4675// Remove all listeners
4776const removeListeners = () => {
4877 Motion .removeAllListeners ();
@@ -51,7 +80,10 @@ const removeListeners = () => {
5180
5281See the
5382[ ` DeviceMotionEvent ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent )
54- API to understand the data supplied in the 'accel' event.
83+ and
84+ [ ` DeviceOrientationEvent ` ] ( https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent )
85+ APIs to understand the data supplied in the ` 'accel' ` and ` 'orientation' `
86+ events respectively.
5587
5688## API
5789
0 commit comments