Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I request multiple permissions at once in react native

I'd like to request permissions on one page instead of waiting for each particular situation. However, I do not want multiple popups. Is there a way to ask for permissions with a single popup/modal.

On the android side I found this post and this, which look promising, but I have yet to find something for iOS.

like image 799
VK1 Avatar asked Feb 22 '19 03:02

VK1


2 Answers

Makes sure you also add respective permissions in manifest file as well.

export async function GetAllPermissions() {
  try {
    if (Platform.OS === "android") {
      const userResponse = await PermissionsAndroid.requestMultiple([
        PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
        PermissionsAndroid.PERMISSIONS.CALL_PHONE
      ]);
      return userResponse;
    }
  } catch (err) {
    Warning(err);
  }
  return null;
}
like image 189
Navneet kumar Avatar answered Sep 21 '22 05:09

Navneet kumar


In Android

First add permissions in to the AndroidManifest.xml file and then

if (Platform.OS === 'android') {
    PermissionsAndroid.requestMultiple(
      [PermissionsAndroid.PERMISSIONS.CAMERA, 
      PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION,
      PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
      PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE]
      ).then((result) => {
        if (result['android.permission.ACCESS_COARSE_LOCATION']
        && result['android.permission.CAMERA']
        && result['android.permission.READ_CONTACTS']
        && result['android.permission.ACCESS_FINE_LOCATION']
        && result['android.permission.READ_EXTERNAL_STORAGE']
        && result['android.permission.WRITE_EXTERNAL_STORAGE'] === 'granted') {
          this.setState({
            permissionsGranted: true
          });
        } else if (result['android.permission.ACCESS_COARSE_LOCATION']
        || result['android.permission.CAMERA']
        || result['android.permission.READ_CONTACTS']
        || result['android.permission.ACCESS_FINE_LOCATION']
        || result['android.permission.READ_EXTERNAL_STORAGE']
        || result['android.permission.WRITE_EXTERNAL_STORAGE'] === 'never_ask_again') {
          this.refs.toast.show('Please Go into Settings -> Applications -> APP_NAME -> Permissions and Allow permissions to continue');
        }
      });
  }

In iOS In the info section of your project on XCode

  • Add the permissions and the description say - ex: Privacy - Contacts Usage Description then,

    Permissions.request('photo').then(response => {
      if (response === 'authorized') {
        iPhotoPermission = true;
      }
     Permissions.request('contact').then(response => {
      if (response === 'authorized') {
        iPhotoPermission = true;
      }
    });
    });
    
like image 41
Syed Avatar answered Sep 22 '22 05:09

Syed