权限
了解如何在应用配置文件中配置和添加权限。
在开发需要访问用户设备上潜在敏感信息(如位置或联系人)的原生应用时,应用必须首先请求用户的许可。例如,要访问用户的媒体库,应用需要运行 MediaLibrary.requestPermissionsAsync()。
🌐 When developing a native app that requires access to potentially sensitive information on a user's device, such as their location or contacts, the app must request the user's permission first. For example, to access the user's media library, the app will need to run MediaLibrary.requestPermissionsAsync().
在独立应用和开发构建中,权限需要在本地构建时进行配置,才能通过运行时的 JavaScript 代码请求。在使用Expo Go应用测试项目时,则不需要这样做。
🌐 Permissions in standalone and development builds require native build-time configuration before they can be requested using runtime JavaScript code. This is not required when testing projects in the Expo Go app.
如果你没有正确配置或说明原生权限,可能会导致你的应用被拒绝或从应用商店下架。
安卓
🌐 Android
权限通过你的 应用配置 中的 android.permissions 和 android.blockedPermissions 键进行配置。
🌐 Permissions are configured with the android.permissions and android.blockedPermissions keys in your app config.
大多数权限会由你在应用中使用的库自动添加,或者通过 config 插件,或者通过包级别的 AndroidManifest.xml。你只需要使用 android.permissions 来添加库默认未包括的额外权限。
🌐 Most permissions are added automatically by libraries that you use in your app either with config plugins or with a package-level AndroidManifest.xml. You only need to use android.permissions to add additional permissions that are not included by default in a library.
{ "android": { "permissions": ["android.permission.SCHEDULE_EXACT_ALARM"] } }
删除由包级 AndroidManifest.xml 文件添加的权限的唯一方法是使用 android.blockedPermissions 属性来阻止它们。为此,需要指定 完整的权限名称。例如,如果你想删除 expo-camera 添加的音频录制权限:
🌐 The only way to remove permissions that are added by package-level AndroidManifest.xml files is to block them with the android.blockedPermissions property. To do this, specify the full permission name. For example, if you want to remove the audio recording permissions added by expo-camera:
{ "android": { "blockedPermissions": ["android.permission.RECORD_AUDIO"] } }
- 请参阅
android.permissions以了解默认 预构建模板 中包含哪些权限。 - 未经正当理由使用 危险 或 签名 权限的应用 可能会被 Google 拒绝。在提交应用时,请确保遵循Android 权限最佳实践。
- 所有可用的 Android
Manifest.permissions。
Are you using this library in an existing React Native app?
修改 AndroidManifest.xml 以排除特定权限:在 <use-permission> 标签中添加 tools:node="remove" 属性,以确保它被移除,即使它包含在库的 AndroidManifest.xml 中。
🌐 Modify AndroidManifest.xml to exclude specific permissions: add the tools:node="remove" attribute to a <use-permission> tag to ensure it is removed, even if it's included in a library's AndroidManifest.xml.
<manifest xmlns:tools="http://schemas.android.com/tools"> <uses-permission tools:node="remove" android:name="android.permission.ACCESS_FINE_LOCATION" /> </manifest>
你必须先在
<manifest>上定义xmlns:tools属性,然后才能在权限上使用tools:node属性。
iOS
你的 iOS 应用可以向用户请求系统权限。例如,要使用设备的相机或访问照片,Apple 需要说明你的应用如何使用这些数据。大多数软件包会自动为特定权限提供模板原因,通过 config plugins 设置。这些默认消息很可能需要根据你的具体用例进行调整,才能让你的应用被 App Store 接受。
🌐 Your iOS app can ask for system permissions from the user. For example, to use the device's camera or access photos, Apple requires an explanation for how your app makes use of that data. Most packages will automatically provide a boilerplate reason for a given permission with config plugins. These default messages will most likely need to be tailored to your specific use case for your app to be accepted by the App Store.
要设置权限消息,请在你的应用配置中使用ios.infoPlist键,例如:
🌐 To set permission messages, use the ios.infoPlist key in your app config, for example:
{ "ios": { "infoPlist": { "NSCameraUsageDescription": "This app uses the camera to scan barcodes on event tickets." } } }
这些属性中的许多也可以通过与添加它们的库相关的 config 插件 属性直接配置。例如,使用 expo-media-library 你可以像这样配置照片权限消息:
🌐 Many of these properties are also directly configurable using the config plugin properties associated with the library that adds them. For example, with expo-media-library you can configure photo permission messages like this:
{ "plugins": [ [ "expo-media-library", { "photosPermission": "Allow $(PRODUCT_NAME) to access your photos.", "savePhotosPermission": "Allow $(PRODUCT_NAME) to save photos." } ] ] }
- 对 Info.plist 的更改无法通过 OTA 更新,只能在提交新的原生二进制文件时部署。例如,通过
eas build。 - 苹果官方的权限消息推荐。
- 所有可用的 Info.plist 属性。
Are you using this library in an existing React Native app?
直接在 Info.plist 文件中添加和修改权限信息提示值。我们建议直接在 Xcode 中进行操作,以便获得自动补全功能。
🌐 Add and modify the permission message values in Info.plist file directly. We recommend doing this directly in Xcode for autocompletion.
Web
在网页上,像 Camera 和 Location 这样的权限只能从安全上下文请求。例如,使用 https:// 或 http://localhost。此限制类似于 Android 的清单权限以及 iOS 的 Info.plist 使用提示信息,并且此措施是为了增强隐私保护。
🌐 On the web, permissions like the Camera and Location can only be requested from a secure context. For example, using https:// or http://localhost. This limitation is similar to Android's manifest permissions and iOS's Info.plist usage messages and is enforced to increase privacy.
重置权限
🌐 Resetting permissions
通常你希望能够测试当用户拒绝权限时会发生什么,以确保你的应用能够优雅地应对。在 Android 和 iOS 上,操作系统级别的限制禁止应用多次请求相同的权限(你可以想象,如果用户拒绝后不断被提示权限,会有多烦人)。为了在开发中测试涉及权限的不同流程,你可能需要卸载并重新安装原生应用。
🌐 Often you want to be able to test what happens when a user rejects permissions, to ensure your app reacts gracefully. An operating-system level restriction on both Android and iOS prohibits an app from asking for the same permission more than once (you can imagine how this could be annoying for the user to be repeatedly prompted for permissions after rejecting them). To test different flows involving permissions in development, you may need to uninstall and reinstall the native app.
在 Expo Go 中进行测试时,你可以通过在 Expo CLI 终端界面中运行 npx expo start 并按 i 或 a 来删除并重新安装应用。