首页指南参考教程

Expo TrackingTransparency

一个库,用于请求跟踪使用 iOS 14 及更高版本的设备上的用户的权限。

Android
iOS

用于请求跟踪用户或其设备的权限的库。用于跟踪的数据示例包括电子邮件地址、设备 ID、广告 ID 等。仅在 iOS 14 及更高版本上需要此权限;在 iOS 13 及更低版本上,始终授予此权限。如果 "允许应用请求跟踪" 设备级别设置为关闭,则此权限将被拒绝。请务必将 NSUserTrackingUsageDescription 添加到 Info.plist 以解释如何跟踪用户。否则,你的应用将被苹果拒绝。

¥A library for requesting permission to track the user or their device. Examples of data used for tracking include email address, device ID, advertising ID, and more. This permission is only necessary on iOS 14 and higher; on iOS 13 and below this permission is always granted. If the "Allow Apps to Request to Track" device-level setting is off, this permission will be denied. Be sure to add NSUserTrackingUsageDescription to your Info.plist to explain how the user will be tracked. Otherwise, your app will be rejected by Apple.

有关 Apple 新的应用跟踪透明度框架的更多信息,请参阅他们的 documentation

¥For more information on Apple's new App Tracking Transparency framework, please refer to their documentation.

安装

¥Installation

Terminal
npx expo install expo-tracking-transparency

If you are installing this in an existing React Native app (bare workflow), start by installing expo in your project. Then, follow the additional instructions as mentioned by library's README under "Installation in bare React Native projects" section.

app.json/app.config.js 中的配置

¥Configuration in app.json/app.config.js

如果你在项目中使用配置插件(EAS 构建npx expo run:[android|ios]),则可以使用其内置的 配置插件 配置 expo-tracking-transparency。该插件允许你配置无法在运行时设置的各种属性,并且需要构建新的应用二进制文件才能生效。

¥You can configure expo-tracking-transparency using its built-in config plugin if you use config plugins in your project (EAS Build or npx expo run:[android|ios]). The plugin allows you to configure various properties that cannot be set at runtime and require building a new app binary to take effect.

Are you using this library in a bare React Native app?

了解如何在 expo-tracking-transparency 存储库中的安装说明 文件中配置原生项目。

¥Learn how to configure the native projects in the installation instructions in the expo-tracking-transparency repository.

Example app.json with config plugin

app.json
{
  "expo": {
    "plugins": [
      [
        "expo-tracking-transparency",
        {
          "userTrackingPermission": "This identifier will be used to deliver personalized ads to you."
        }
      ]
    ]
  }
}

Configurable properties

NameDefaultDescription
userTrackingPermission"Allow this app to collect app-related data that can be used for tracking you or your device."
Only for:
iOS

Sets the iOS NSUserTrackingUsageDescription permission message in Info.plist.

用法

¥Usage

Basic tracking transparency usage
import React, { useEffect } from 'react';
import { Text, StyleSheet, View } from 'react-native';
import { requestTrackingPermissionsAsync } from 'expo-tracking-transparency';

export default function App() {
  useEffect(() => {
    (async () => {
      const { status } = await requestTrackingPermissionsAsync();
      if (status === 'granted') {
        console.log('Yay! I have user permission to track data');
      }
    })();
  }, []);

  return (
    <View style={styles.container}>
      <Text>Tracking Transparency Module Example</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

API

import * as ExpoTrackingTransparency from 'expo-tracking-transparency';

Hooks

useTrackingPermissions(options)

NameType
options
(optional)
PermissionHookOptions<object>

Check or request the user to authorize or deny access to app-related data that can be used for tracking the user or the device. Examples of data used for tracking include email address, device ID, advertising ID, etc. On iOS 14.5 and above, if the user denies this permission, any attempt to collect the IDFA will return a string of 0s.

The system remembers the user’s choice and doesn’t prompt again unless a user uninstalls and then reinstalls the app on the device.

On Android, web, and iOS 13 and below, this method always returns that the permission was granted.

Returns:

[null | PermissionResponse, RequestPermissionMethod<PermissionResponse>, GetPermissionMethod<PermissionResponse>]

Example

const [status, requestPermission] = useTrackingPermissions();

Methods

getAdvertisingId()

Gets the advertising ID, a UUID string intended only for advertising. Use this string for frequency capping, attribution, conversion events, estimating the number of unique users, advertising fraud detection, and debugging.

As a best practice, don't store the advertising ID. Instead, call this function each time your app needs to use the advertising ID. Users can change whether they allow app tracking and can reset their advertising ID at any time in their system settings. Check your app's authorization using getTrackingPermissionsAsync() to determine the user's intent.

On Android, this function returns the "Android Advertising ID" (AAID). On Android devices that support multiple users, including guest users, it's possible for your app to obtain different advertising IDs on the same device. These different IDs correspond to different users who could be signed in on that device. See Google's documentation for more information: Get a user-resettable advertising ID.

On iOS, this function returns the "Identifier for Advertisers" (IDFA), a string that's unique to each device. On devices running iOS 14.5 and newer, your app must request tracking authorization using requestTrackingPermissionsAsync() before it can get the advertising identifier.

Returns:

string | null

Returns either a UUID string or null. It returns null in the following cases:

  • On Android, when isLimitAdTrackingEnabled() is true
  • In the iOS simulator, regardless of any settings
  • On devices running iOS 14.5 and later if you haven't received permission using requestTrackingPermissionsAsync()
  • On iOS, if you've requested permission and the user declines
  • On iOS, when a profile or configuration restricts access to the advertising identifier, such as when the user has turned off the system-wide "Allow Apps to Request to Track" setting

Example

TrackingTransparency.getAdvertisingId();
// "E9228286-4C4E-4789-9D95-15827DCB291B"

getTrackingPermissionsAsync()

Checks whether or not the user has authorized the app to access app-related data that can be used for tracking the user or the device. See requestTrackingPermissionsAsync for more details.

On Android, web, and iOS 13 and below, this method always returns that the permission was granted.

Returns:

Promise<PermissionResponse>

Example

const { granted } = await getTrackingPermissionsAsync();

if (granted) {
  // Your app is authorized to track the user or their device
}

isAvailable()

Returns whether the TrackingTransparency API is available on the current device.

Returns:

boolean

Currently this is true on iOS 14 and above only. On devices where the Tracking Transparency API is unavailable, the get and request permissions methods will always resolve to granted.

requestTrackingPermissionsAsync()

Requests the user to authorize or deny access to app-related data that can be used for tracking the user or the device. Examples of data used for tracking include email address, device ID, advertising ID, etc. On iOS 14.5 and above, if the user denies this permission, any attempt to collect the IDFA will return a string of 0s.

The system remembers the user’s choice and doesn’t prompt again unless a user uninstalls and then reinstalls the app on the device.

On Android, web, and iOS 13 and below, this method always returns that the permission was granted.

Returns:

Promise<PermissionResponse>

Example

const { granted } = await requestTrackingPermissionsAsync();

if (granted) {
  // Your app is authorized to track the user or their device
}

Interfaces

PermissionResponse

An object obtained by permissions get and request functions.

PermissionResponse Properties

NameTypeDescription
canAskAgainboolean

Indicates if user can be asked again for specific permission. If not, one should be directed to the Settings app in order to enable/disable the permission.

expiresPermissionExpiration

Determines time when the permission expires.

grantedboolean

A convenience boolean that indicates if the permission is granted.

statusPermissionStatus

Determines the status of the permission.


Types

PermissionExpiration

Literal Type: multiple types

Permission expiration time. Currently, all permissions are granted permanently.

Acceptable values are: 'never' | number

PermissionHookOptions

Literal Type: multiple types

Acceptable values are: PermissionHookBehavior | Options

Enums

PermissionStatus

PermissionStatus Values

DENIED

PermissionStatus.DENIED = "denied"

User has denied the permission.

GRANTED

PermissionStatus.GRANTED = "granted"

User has granted the permission.

UNDETERMINED

PermissionStatus.UNDETERMINED = "undetermined"

User hasn't granted or denied the permission yet.

权限

¥Permissions

安卓

¥Android

Android PermissionDescription

iOS 系统

¥iOS

该库使用以下用法描述键:

¥The following usage description keys are used by this library:

Info.plist KeyDescription

NSUserTrackingUsageDescription

A message that informs the user why an app is requesting permission to use data for tracking the user or the device.
Expo 中文网 - 粤ICP备13048890号