This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Expo 年龄范围
一个库,可使用 Android 上的 Play Age Signals API 和 iOS 上的 Declared Age Range 框架访问年龄范围信息。
expo-age-range 提供对用户年龄范围信息的访问。它在 Android 上使用 Google 的 Play Age Signals API,在 iOS 上使用 Apple 的 Declared Age Range framework。
此库允许你向应用用户请求年龄范围信息,以帮助你遵守适龄内容法规(例如在 美国德克萨斯州)并在应用中提供适龄体验。
重要 Google 和 Apple 提供的底层原生 API 正在积极开发中。虽然此库已稳定,但为适应这些 API,可能需要进行更多破坏性更改。
限制
我们建议在真实设备上测试此功能,因为模拟器运行时可能无法按预期工作。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
在应用配置中进行配置
设置 iOS 项目
要在 iOS 上使用年龄范围 API,你需要使用 Xcode 26.0 或更高版本构建项目,但我们建议使用最新版本的 Xcode,以便访问最新的 API。
需要使用 com.apple.developer.declared-age-range 权限。将其添加到你的应用配置文件中:
Are you using this library in an existing React Native app?
对于现有的 React Native 项目,将该权限添加到项目的 ios/[app]/[app].entitlements 文件中:
<key>com.apple.developer.declared-age-range</key> <true/>
用法
在 Android 上,Play Age Signals 仅在用户同意共享年龄范围时才会报告年龄范围。请先调用 requestAgeSignalsAccessAsync,并仅在该调用解析为 'SHARED' 时继续。在 iOS 上,同意提示是 requestAgeRangeAsync 的一部分,因此该调用会解析为 null,下面的示例会继续执行。
其他资源
- Play 年龄信号 API:关于年龄信号的 Android 文档
- 已声明年龄范围框架:关于已声明年龄范围的 iOS 文档。
API
import * as AgeRange from 'expo-age-range';
Methods
Returns the set of regulatory features that the OS reports as required for the current user.
Use this to discover which age-assurance obligations apply.
Resolves with null on iOS earlier than 26.4 and on Android and web — treat
null as "unknown" rather than "no features required".
Promise<AgeRangeRegulatoryFeature[] | null>Asks the OS whether age-assurance regulation applies to the current user. Apple uses this to signal that the account region is covered by a law such as Utah's or Louisiana's age-assurance requirements, so apps can avoid gating users in jurisdictions where the rules do not apply.
- Resolves with
trueonly when Apple confirms regulation applies. - Resolves with
falsewhen the OS confirms regulation does not apply. - Resolves with
nullon iOS earlier than 26.2, and on Android and web. Treatnullas "unknown" rather than a definitivefalse. - Rejects when the request fails — see AgeRangeService.Error
for more information. Treat rejection as "unknown" and fall through to
requestAgeRangeAsyncor your own gating logic.
Recommended pattern: call this first and only prompt the user for their age
range when the result is not false. When it is false, the user is outside
a regulated jurisdiction and you can skip the age gate entirely.
Promise<boolean | null>Example
try { const eligible = await isEligibleForAgeFeaturesAsync(); if (eligible === false) { // Regulation does not apply — no age gate needed. return; } } catch { // Treat errors as "unknown" and fall through to the prompt below or your own gating logic. } const ageRange = await requestAgeRangeAsync({ threshold1: 18 });
Prompts the user to share their age range with the app. Responses may be cached by the OS for future requests.
Promise<AgeRangeResponse>A promise that resolves with user's age range response, or rejects with an error.
The user needs to be signed in on the device to get a valid response.
When not supported (earlier than iOS 26 and web), the call returns lowerBound: 18, which is equivalent to the response of an adult user.
On Android, call requestAgeSignalsAccessAsync first and
only call this function when it resolves with 'SHARED'. Play Age Signals reports every field as
null otherwise.
Asks the user to consent to sharing their age signals, showing the Play Age Signals in-app age sharing consent
screen. Play Age Signals requires this before requestAgeRangeAsync:
age signals are only reported while the status is 'SHARED'.
- Resolves with
'SHARED'when the user agrees to share their age signals. Only then doesrequestAgeRangeAsyncreport an age range. - Resolves with
'NOT_SHARED'when the user does not agree.requestAgeRangeAsyncreports every field asnulluntil the user consents. - Resolves with
'VERIFICATION_REQUIRED'when the user's age is unknown and they are in a region where age verification is mandatory. Ask the user to visit the Play Store to resolve their status. - Resolves with
nullwhen Play Age Signals reports no status, and on iOS and web. On iOS the consent prompt is part ofrequestAgeRangeAsyncitself, so there is nothing separate to call. - Rejects when the request fails.
Promise<AgeSignalsStatus | null>Displays a system-provided interface for people to acknowledge a significant app update.
Only on iOS 26.4+, this presents an update acknowledgement dialog and resolves once the user confirms it, or rejects with an error. On unsupported platforms this resolves immediately without showing any UI.
Call getRequiredRegulatoryFeaturesAsync first to
determine whether the user actually needs to acknowledge a significant change — only invoke
this function when the returned features include 'significantAppChangeRequiresAdultNotification'.
Doing so avoids prompting users who are not subject to the regulation.
Promise<void>Types
Literal type: string
A regulatory feature that your app may need to support for the current user.
Mirrors AgeRangeService.RegulatoryFeature.
Acceptable values are: 'declaredAgeRangeRequired' | 'significantAppChangeRequiresAdultNotification' | 'significantAppChangeRequiresParentalConsent'
Response containing the user's age range information.
Contains age boundaries and platform-specific metadata.
Literal type: string
The sharing status of age signals, returned by requestAgeSignalsAccessAsync.
Acceptable values are: 'SHARED' | 'NOT_SHARED' | 'VERIFICATION_REQUIRED'
错误代码
可在原生模块抛出的任何错误的 code 属性中找到。有关 Android 特有的错误代码,请参阅 Use Play Age Signals API 文档中的“错误代码参考”。