This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

Expo AgeRange

一个库,通过 Android 上的 Play Age Signals API 和 iOS 上的 Declared Age Range 框架提供年龄范围信息访问。

Android
iOS
Included in Expo Go

expo-age-range 提供用户年龄范围信息的访问权限。它在 Android 上使用 Google 的 Play Age Signals API,在 iOS 上使用 Apple 的 Declared Age Range framework。

这个库允许你从应用用户那里请求年龄范围信息,以帮助你遵守适龄内容规定(例如在美国德克萨斯州)并在你的应用中提供适合年龄的体验。

🌐 This library allows you to request age range information from your app users to help you comply with age-appropriate content regulations (such as in Texas, USA) and provide age-appropriate experiences in your app.

局限性

🌐 Limitations

我们建议在真实设备上测试功能,因为模拟器运行时可能无法按预期工作。

🌐 We recommend testing the functionality on a real device, as simulator runtimes may not work as expected.

安装

🌐 Installation

Terminal
- npx expo install expo-age-range
- yarn expo install expo-age-range
- pnpm expo install expo-age-range
- bun expo install expo-age-range

If you are installing this in an existing React Native app, make sure to install expo in your project.

应用配置中的配置

🌐 Configuration in app config

搭建 iOS 项目

🌐 Set up the iOS project

要在 iOS 上使用年龄范围 API,你需要用 Xcode 26.0 或更高版本构建你的项目,但我们建议你使用最新的 Xcode 进行构建,以便可以使用最新的 API。

🌐 To use the age range API on iOS, you need to build your project with Xcode 26.0 or later but we recommend you build with the latest Xcode to have access to the latest APIs.

需要 com.apple.developer.declared-age-range 权限。将它添加到你的 应用配置 文件中:

🌐 The com.apple.developer.declared-age-range entitlement is required. Add it to your app config file:

app.json
{ "expo": { "ios": { "entitlements": { "com.apple.developer.declared-age-range": true } } } }
Are you using this library in an existing React Native app?

对于现有的 React Native 项目,将权限添加到项目的 ios/[app]/[app].entitlements 文件中:

🌐 For existing React Native projects, add the entitlement to your project's ios/[app]/[app].entitlements file:

<key>com.apple.developer.declared-age-range</key> <true/>

用法

🌐 Usage

在安卓系统上,Play Age Signals 只有在用户同意分享时才会报告年龄范围。首先调用 requestAgeSignalsAccessAsync,只有在它返回 'SHARED' 后才继续。在 iOS 上,同意提示是 requestAgeRangeAsync 的一部分,因此调用会返回 null,下面的示例就会继续。

🌐 On Android, Play Age Signals only reports an age range while the user consents to sharing it. Call requestAgeSignalsAccessAsync first and continue only when it resolves with 'SHARED'. On iOS the consent prompt is part of requestAgeRangeAsync, so the call resolves with null and the example below continues.

Age Range usage
import * as AgeRange from 'expo-age-range'; import { useState } from 'react'; import { StyleSheet, Text, View, Button } from 'react-native'; export default function App() { const [result, setResult] = useState<AgeRange.AgeRangeResponse | { error: string } | null>(null); const requestAgeRange = async () => { try { // On Android, ask the user to share their age signals first. Resolves with null on iOS. const status = await AgeRange.requestAgeSignalsAccessAsync(); if (status !== null && status !== 'SHARED') { setResult({ error: `Age signals are not shared: ${status}` }); return; } const ageRange = await AgeRange.requestAgeRangeAsync({ threshold1: 10, threshold2: 13, threshold3: 18, }); setResult(ageRange); } catch (error) { setResult({ error: error instanceof Error ? error.message : String(error) }); } }; return ( <View style={styles.container}> <Button title="请求年龄范围" onPress={requestAgeRange} /> {result && ( <Text style={styles.result}> {'error' in result ? `Error: ${result.error}` : `Lower age bound: ${result.lowerBound}`} </Text> )} </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', padding: 20, }, result: { marginTop: 20, fontSize: 16, }, });

在安卓上测试年龄信号

🌐 Testing age signals on Android

Google Play 只会向已启用的账户报告年龄信号,所以你的测试账户可能没有你需要的年龄范围。你可以通过 Google Play 的 FakeAgeSignalsManager 使用 setFakeAgeSignals 来伪造它们。

🌐 Google Play only reports age signals to accounts it has enabled, so your test accounts may not have the age range you need. Use setFakeAgeSignals to fake them instead, through Google Play's FakeAgeSignalsManager.

只有可调试的构建才能伪造信号,因为伪造的信号会让应用中的任何代码绕过你的年龄限制。在不可调试的构建中传递除了 null 以外的任何内容都会抛出 ERR_AGE_RANGE_FAKE_SIGNALS_NOT_DEBUGGABLE。

🌐 Only debuggable builds can fake signals, because faked signals would let any code in the app bypass your age gating. Passing anything other than null in a build that is not debuggable throws ERR_AGE_RANGE_FAKE_SIGNALS_NOT_DEBUGGABLE.

你的请求不会变,只有它们报告的内容会变:

🌐 Your requests do not change, only what they report:

// Report a supervised 13 to 15 year old with a change waiting for approval. AgeRange.setFakeAgeSignals({ ageSignalsStatus: 'SHARED', lowerBound: 13, upperBound: 15, ageRangeSource: 'TIER_B', significantChangeStatus: 'PENDING', }); const status = await AgeRange.requestAgeSignalsAccessAsync(); const { lowerBound } = await AgeRange.requestAgeRangeAsync({ threshold1: 18 }); // Report the real signals again. AgeRange.setFakeAgeSignals(null);

要伪造一个失败,传递一个 错误代码 而不是信号:

🌐 To fake a failure, pass an error code instead of signals:

AgeRange.setFakeAgeSignals({ errorCode: -4 });

其他资源

🌐 Additional resources

应用接口

🌐 API

import * as AgeRange from 'expo-age-range';

No API data file found, sorry!

错误代码

🌐 Error codes

在本地模块抛出的任何错误的 code 属性中可用。有关 Android 特定的错误代码,请参阅 使用 Play Age Signals API 文档 中的“错误代码参考”。

🌐 Available in the code property of any error thrown by the native module. For Android-specific error codes, see the "Error code reference" in Use Play Age Signals API docs.

代码平台描述
ERR_AGE_RANGE_USER_DECLINED
iOS
用户拒绝分享他们的年龄范围。
ERR_AGE_RANGE_NOT_AVAILABLE
iOS
年龄范围不可用。最可能的原因是用户未在设备上登录他们的 Apple 账户。
ERR_AGE_RANGE_INVALID_REQUEST
iOS
提供的参数无效。年龄范围至少需相隔 2 年。
ERR_AGE_RANGE_TASK_CANCELLED
Android
用户关闭了 Play Age Signals 年龄共享同意界面。
ERR_AGE_RANGE_FAKE_SIGNALS_CONFLICT
Android
setFakeAgeSignals 同时传入了 errorCode 和年龄信号。
ERR_AGE_RANGE_FAKE_SIGNALS_NOT_DEBUGGABLE
Android
setFakeAgeSignals 被要求在不可调试的版本中伪造信号。