我如何在React Native中一次请求多个权限 [英] How do I request multiple permissions at once in react native

查看:757
本文介绍了我如何在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.

在android方面,我发现了帖子

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

推荐答案

在Android中

首先将权限添加到AndroidManifest.xml文件中,然后

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');
        }
      });
  }

在iOS中 在您的XCode项目的信息部分

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;
  }
});
});

这篇关于我如何在React Native中一次请求多个权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆