【React Native】请求设备权限

本文介绍了在React Native(RN)应用中如何请求用户设备权限,包括安装`react-native-permissions`库,连接原生模块,以及在iOS和Android上处理权限状态。详细步骤包括在XCode中初始化和加载库,以及注意iOS的权限描述规范。同时,文章提到了权限请求的方法和特定权限的选项,例如location和notification权限的请求方式。
摘要由CSDN通过智能技术生成

React Native请求用户权限

Request user permissions from React Native, iOS + Android

参考链接:react-native-permissions - npm

一、如何使用

1.1 添加组件库:npm install --save react-native-permissions --- or ---  yarn add react-native-permissions

1.2 连接原生库:react-native link react-native-permissions

1.3 iOS端初始化内库:In the XCode's "Project navigator", right click on your project's Libraries folder ➜ Add Files to <...>

1.4 iOS端加载组件库:Go to node_modules ➜ react-native-permissions ➜ selectReactNativePermissions.xcodeproj

1.5 iOS端加载内库: Add libReactNativePermissions.a to Build Phases -> Link Binary With Libraries

1.6 Android端现在在Android上使用React Native自己的JS PermissionsAndroid模块,不再需要在Android上做任何额外的连接

  • 注意:Don't forget to add permissions to AndroidManifest.xml for android and Info.plistfor iOS (Xcode >= 8). See iOS Notes or Android Notes for more details. 
// example

    Permissions.check('location', { type: 'always' }).then(response => {
        this.setState({ locationPermission: response })
    })
 
    Permissions.request('location', { type: 'always' }).then(response => {
        this.setState({ locationPermission: response })
    })
 
    Permissions.request('notification', { type: ['alert', 'badge'] }).then(
        response => {
        this.setState({ notificationPermission: response })
        },
    )

二、权限状态

Promises resolve into one of these statuses:

Return valueNotes
authorized

User has authorized this permission

用户已授权此权限

denied

User has denied this permission at least once.

On iOS this means that the user will not be prompted again.

Android users can be prompted multiple times until they select 'Never ask me again'

用户至少一次拒绝此权限。

在iOS上,这意味着用户不会再次被提示。

安卓用户可以被多次提示,直到他们选择“永远不要再问我”

restricted

iOS - this means user is not able to grant this permission, either because it's not supported by the device or because it has been blocked by parental controls. 

Android - this means that the user has selected 'Never ask me again' while denying permission

iOS - 这意味着用户不能授予这个权限,要么是因为设备不支持它,要么是因为它被父母的控制阻止了。

Android - 这意味着用户在拒绝许可的同时选择了“永远不要再问我”

undetermined

User has not yet been prompted with a permission dialog

用户还没有被提示使用权限对话框

三、方法

Method NameArgumentsNotes
check()type- Returns a promise with the permission status. See iOS Notes for special cases
request()type- Accepts any permission type except backgroundRefresh. If the current status is undetermined, shows the permission dialog and returns a promise with the resulting status. Otherwise, immediately return a promise with the current status. See iOS Notes for special cases
checkMultiple()[types]- Accepts an array of permission types and returns a promise with an object mapping permission types to statuses
getTypes()none- Returns an array of valid permission types
openSettings()none(iOS only - 8.0 and later) Switches the user to the settings page of your app
canOpenSettings()none(iOS only) Returns a boolean indicating if the device supports switching to the settings page

四、权限支持情况

TypeiOSAndroid
Locationlocation✔️
Cameracamera✔️
Microphonemicrophone✔️
Photosphoto✔️
Contactscontacts✔️
Eventsevent✔️
Bluetoothbluetooth✔️
Remindersreminder✔️
Push Notificationsnotification✔️
Background RefreshbackgroundRefresh✔️
Speech RecognitionspeechRecognition✔️
mediaLibrarymediaLibrary✔️
Motion Activitymotion✔️
Storagestorage❌️
Phone CallcallPhone❌️
Read SMSreadSms❌️
Receive SMSreceiveSms❌️

五、注意

(1)location权限请求request() 和检查 check() 支持第二个参数,字符串类型,表示总是使用或者在使用时使用(默认项)

(2)notification权限请求request() 支持第二个参数,是具有所需警报类型的数组类型,表示警报、徽章和声音的任何组合(默认是这三种请求都请求)

(3)iOS端的权限描述必须规范,必须指定为什么应用程序请求该权限。例:‘ APP会在上传头像图片等服务中访问您的相册权限,是否允许?’、’APP需要您的同意,才能访问相机进行拍照/扫描二维码,如禁止将无法拍照/扫描二维码‘。官方语句:please revise the permission modal alert to specify why the app is requesting access to the camera and location

(4)……

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZhangKui_c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值