React Native 请求设备权限

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZhangKui0418/article/details/82886191

React Native请求用户权限

Request user permissions from React Native, iOS + Android

参考链接:https://www.npmjs.com/package/react-native-permissions

一、如何使用

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 value Notes
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 Name Arguments Notes
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

四、权限支持情况

  Type iOS Android
Location location ✔️
Camera camera ✔️
Microphone microphone ✔️
Photos photo ✔️
Contacts contacts ✔️
Events event ✔️
Bluetooth bluetooth ✔️
Reminders reminder ✔️
Push Notifications notification ✔️
Background Refresh backgroundRefresh ✔️
Speech Recognition speechRecognition ✔️
mediaLibrary mediaLibrary ✔️
Motion Activity motion ✔️
Storage storage ❌️
Phone Call callPhone ❌️
Read SMS readSms ❌️
Receive SMS receiveSms ❌️

五、注意

(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)……

猜你喜欢

转载自blog.csdn.net/ZhangKui0418/article/details/82886191