Handling permissions

React-Native Zendrive provides a permissions module to deal with various permissions that Zendrive SDK requires.

Location Permission

1. Check for Location Access

Use the following code snippet check whether SDK has access to the user's location.

Zendrive.permissions()
  .check('location')
  .then(hasAccess => {
    if (hasAccess) {
      console.log('location access granted');
    } else {
      // request access
    }
  });

In Android, the permissions module will check access for the following:

android.permission.ACCESS_FINE_LOCATION

android.permission.ACCESS_BACKGROUND_LOCATION

in iOS, the module will check 'always allow' granted access for location.

2. Request Location Access

Use the following code snippet to request location access. Note that here we are using async/await to simplify code.

const hasAccess = await Zendrive.permissions().request('location');
if (hasAccess) {
  // user granted access, continue with your business logic
} else {
  // user has denied, show a proper error message and provide call to action to open settings
  await Zendrive.permissions().openSettings();
}

In iOS, if the user denies location access, or denies access and checks the Never ask again option, you can enable location from the Settings menu on the application. Use the openSettings method in the permissions module to take the user to application settings.

Motion Permission

1. Check for Motion Access

Use the following code snippet to check whether Zendrive SDK has access to motion.

Zendrive.permissions()
  .check('motion')
  .then(hasAccess => {
    if (hasAccess) {
      console.log('motion access granted');
    } else {
      // request access
    }
  });

In Android, the permissions module will check for motion access through the android.permission.ACTIVITY_RECOGNITION call.

In iOS >= 11, the permissions module will check for motion access authorization equivalent to CMMotionActivityManager.authorizationStatus()

2. Request Motion Access

Zendrive SDK will automatically prompt for motion access when required. Please make sure that NSMotionUsageDescription is provided in Info.plist.

Use the following code snippet to request motion access. Note that here we are using async/await to simplify code:

const hasAccess = await Zendrive.permissions().request('motion');
if (hasAccess) {
  // user granted access, continue with your business logic
} else {
  // user has denied, show a proper error message and provide call to action to open settings
  await Zendrive.permissions().openSettings();
}

In Android, if the users denies motion access and checks the Never ask again option, you can enable the permission from application settings. Use openSettings method in the permissions module to take the user to application settings.

Overlay

Overlay permission is applicable only to Android.

1. Check Overlay Access

Use the following code snippet check whether Zendrive SDK has overlay permission; that is, the application will stay on top of all other applications on the user's device.

Zendrive.permissions()
  .check('overlay')
  .then(hasAccess => {
    if (hasAccess) {
      console.log('overlay access granted');
    } else {
      // request access
    }
  });

The permissions module will check access for the following:android.permission.SYSTEM_ALERT_WINDOW

2. Request Overlay Access

Use the following code snippet to request overlay access. Note that here we are using async/await to simplify code:

const hasAccess = await Zendrive.permissions().request('overlay');
if (hasAccess) {
  // user granted access, continue with your business logic
} else {
  // user has not enabled draw overlay setting, show a proper error message and provide call to action to open settings
  await Zendrive.permissions().openSettings();
}

This code snippet will take the user directly to draw overlay permission screen in application settings, where they can choose to enable or disable permission. When the user returns to the application again, the permission will resolve to true or false depending on the action taken in the Overlay permission screen.

Last updated

Was this helpful?