什么是 React Native 权限以及如何使用它?
What is React Native Permissions & how to use it?
如何使用React Native权限库?
我已经开始学习它告诉我使用库的最佳实践?
应用权限通过保护对以下内容的访问来帮助保护用户隐私:
受限数据,例如系统状态和用户联系信息。
限制操作,例如连接到配对设备和录制音频。
react-native-permissions
iOS、Android 和 Windows 上 React Native 的统一权限 API。
您可以通过以下命令从终端安装它
$ npm install --save react-native-permissions
--- 或 ---
$纱加react-native-permissions
https://github.com/zoontek/react-native-permissions#readme
了解权限流程
由于 iOS 和 Android 上的权限处理方式不同,此库提供了对两个平台行为的抽象。 (在此处查看详细信息)。
更新 Pod 文件以安装权限处理程序。
默认情况下没有安装权限处理程序。
更新您的 Podfile,方法是选择您要检查或请求的文件,然后 运行pod 安装。
Example
target 'YourAwesomeProject' do
# …
permissions_path = '../node_modules/react-native-permissions/ios'
pod 'Permission-AppTrackingTransparency', :path => "#{permissions_path}/AppTrackingTransparency"
pod 'Permission-BluetoothPeripheral', :path => "#{permissions_path}/BluetoothPeripheral"
pod 'Permission-Calendars', :path => "#{permissions_path}/Calendars"
pod 'Permission-Camera', :path => "#{permissions_path}/Camera"
pod 'Permission-Contacts', :path => "#{permissions_path}/Contacts"
end
然后用想要的权限使用说明更新您的Info.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Keep only the permissions used in your app -->
<key>NSAppleMusicUsageDescription</key>
<string>YOUR TEXT</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>YOUR TEXT</string>
<key>YOUR-PURPOSE-KEY</key>
</dict>
</dict>
</plist>
对于Android
将所有想要的权限添加到您的
应用程序 android/app/src/main/AndroidManifest.xml 文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myawesomeapp">
<!-- Keep only the permissions used in your app -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"
/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<!-- … -->
</manifest>
然后像下面这样使用它
import {check, PERMISSIONS, RESULTS} from 'react-native-permissions';
check(PERMISSIONS.IOS.LOCATION_ALWAYS)
.then((result) => {
switch (result) {
case RESULTS.UNAVAILABLE:
console.log('This feature is not available (on this device / in
this context)');
break;
case RESULTS.DENIED:
console.log('The permission has not been requested / is denied
but requestable');
break;
case RESULTS.LIMITED:
console.log('The permission is limited: some actions are
possible');
break;
case RESULTS.GRANTED:
console.log('The permission is granted');
break;
case RESULTS.BLOCKED:
console.log('The permission is denied and not requestable
anymore');
break;
}
})
.catch((error) => {
// …
});
如何使用React Native权限库? 我已经开始学习它告诉我使用库的最佳实践?
应用权限通过保护对以下内容的访问来帮助保护用户隐私:
受限数据,例如系统状态和用户联系信息。
限制操作,例如连接到配对设备和录制音频。 react-native-permissions iOS、Android 和 Windows 上 React Native 的统一权限 API。 您可以通过以下命令从终端安装它
$ npm install --save react-native-permissions
--- 或 ---
$纱加react-native-permissions
https://github.com/zoontek/react-native-permissions#readme
了解权限流程 由于 iOS 和 Android 上的权限处理方式不同,此库提供了对两个平台行为的抽象。 (在此处查看详细信息)。 更新 Pod 文件以安装权限处理程序。 默认情况下没有安装权限处理程序。 更新您的 Podfile,方法是选择您要检查或请求的文件,然后 运行pod 安装。
Example
target 'YourAwesomeProject' do
# …
permissions_path = '../node_modules/react-native-permissions/ios'
pod 'Permission-AppTrackingTransparency', :path => "#{permissions_path}/AppTrackingTransparency"
pod 'Permission-BluetoothPeripheral', :path => "#{permissions_path}/BluetoothPeripheral"
pod 'Permission-Calendars', :path => "#{permissions_path}/Calendars"
pod 'Permission-Camera', :path => "#{permissions_path}/Camera"
pod 'Permission-Contacts', :path => "#{permissions_path}/Contacts"
end
然后用想要的权限使用说明更新您的Info.plist:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- Keep only the permissions used in your app -->
<key>NSAppleMusicUsageDescription</key>
<string>YOUR TEXT</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>YOUR TEXT</string>
<key>YOUR-PURPOSE-KEY</key>
</dict>
</dict>
</plist>
对于Android 将所有想要的权限添加到您的 应用程序 android/app/src/main/AndroidManifest.xml 文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myawesomeapp">
<!-- Keep only the permissions used in your app -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"
/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"
/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<!-- … -->
</manifest>
然后像下面这样使用它
import {check, PERMISSIONS, RESULTS} from 'react-native-permissions';
check(PERMISSIONS.IOS.LOCATION_ALWAYS)
.then((result) => {
switch (result) {
case RESULTS.UNAVAILABLE:
console.log('This feature is not available (on this device / in
this context)');
break;
case RESULTS.DENIED:
console.log('The permission has not been requested / is denied
but requestable');
break;
case RESULTS.LIMITED:
console.log('The permission is limited: some actions are
possible');
break;
case RESULTS.GRANTED:
console.log('The permission is granted');
break;
case RESULTS.BLOCKED:
console.log('The permission is denied and not requestable
anymore');
break;
}
})
.catch((error) => {
// …
});