@approov/approov-service-react-native

中文文档
3.5.18发布于 2026/10/06MITTypeScriptCommonJS零依赖近 30 天下载 3872

Approov Mobile App Protection for ReactNative

原始使用文档
文档目录7 节
  1. Table of Contents
  2. Adding the Approov Dependency
  3. Manifest / Project Changes
  4. Initializing Approov
  5. Using Approov
  6. Checking It Works
  7. Next Steps

Approov Service for React Native

React Native Android iOS npm Approov SDK Message Signing CI

A wrapper for the Approov SDK to enable easy integration when using React Native for making the API calls that you wish to protect with Approov using fetch() or similar. In order to use this you will need a trial or paid Approov account.

Table of Contents

Adding the Approov Dependency

Add the Approov service layer to your existing app with:

npm install @approov/approov-service-react-native

If you experience an error related to peer dependencies, append --force to install with your particular React Native version. The plugin supports version 0.76 or above.

For Expo projects use:

expo install @approov/approov-service-react-native

Manifest / Project Changes

For iOS you must install pod dependencies. Change to the ios directory and run:

pod install

Do not worry if this generates warnings about duplicate UUIDs.

Initializing Approov

Import the service layer:

import { ApproovProvider, ApproovService } from '@approov/approov-service-react-native';

You MUST await successful completion of ApproovService.initialize(config) with your valid Approov configuration before making any protected request. Gate every protected request path, including background tasks and startup effects, on that completion. Android and iOS no longer wait for initialization inside their request interceptors; a request sent before initialization can proceed unprotected. If initialization fails, keep protected requests blocked. Do not rely on a startup delay or polling isInitialized() as a substitute for awaiting the initialization promise.

Initialize explicitly during startup and keep a correlation id in your app logs:

const approovSessionId = global.crypto?.randomUUID?.() || `${Date.now()}-${Math.random()}`;

async function initializeApproov() {
  try {
    await ApproovService.initialize('<enter-your-config-string-here>');

    const enabled = await ApproovService.isApproovEnabled();
    if (enabled) {
      const deviceId = await ApproovService.getDeviceID();
      console.log('Approov initialized', { approovSessionId, deviceId });
    } else {
      throw new Error('Approov protection is not enabled');
    }
  } catch (error) {
    console.error('Approov initialization failed; protected requests remain blocked', {
      approovSessionId,
      error,
    });
    throw error;
  }
}

async function startProtectedRequests() {
  await initializeApproov();
  // Start protected requests only after the await above succeeds.
}

If you prefer component-wrapped startup, wrap your application with ApproovProvider. The same initialization requirement applies: protected requests must remain blocked until initialization completes successfully, including requests started outside the provider's children. Initialization resets the token, binding and substitution headers, exclusion regexes and any service mutator, so apply those in onInitialized, which runs after each successful initialization and before approovReady becomes true. onInit runs before initialization, so anything it sets from that list is lost. See Configuration Is Reset By Initialization:

const approovConfiguration = () => {
  ApproovService.setBindingHeader('Authorization');
};

return (
  <ApproovProvider config="<enter-your-config-string-here>" onInitialized={approovConfiguration}>
    <View>
      <Button onPress={callAPI} title="Press Me!" />
    </View>
  </ApproovProvider>
);

The config string is provided in your Approov onboarding email.

Using Approov

On Android, token processing uses an application interceptor and certificate pinning uses a network interceptor. Clients share the service's pin state: initialization loads pins before its promise resolves, and SDK configuration updates refresh that state for existing clients. Creating clients does not fetch pins. The initial SDK pin fetch can still block initialization. Approov pins replace the client's built-in pinning policy; customer pins are not merged. Every HTTPS network exchange checks the current pins against the certificate chain the trust manager verified, including exchanges on reused connections; these checks do not fetch pins from the SDK.

Once initialization succeeds, network requests may have Approov tokens, message signatures, dynamic pinning, or secure substitutions applied. Initially you will not have set which API domains to protect, so requests are unchanged, but the service will contact the Approov cloud and log UNKNOWN_URL (Android) or unknown URL (iOS).

WebSockets are not supported. A WebSocket connection, including GraphQL subscriptions carried over one, is passed through without an Approov token, secure string substitution or message signature, and Approov does not pin it. Protect only HTTPS requests with Approov; GraphQL queries and mutations sent over HTTPS are protected like any other request. See USAGE.md.

Support is provided for the rn-fetch-blob networking stack through the @approov/rn-fetch-blob fork:

npm uninstall rn-fetch-blob
npm install @approov/rn-fetch-blob

Checking It Works

You may use the ApproovMonitor component, also imported from @approov/approov-service-react-native, inside ApproovProvider. This outputs console logging on the state of Approov initialization.

During initial rollout and whenever you add observability SDKs, capture ApproovService.getPinningDiagnostics() metadata in your app logging:

  • Android: fetch the metadata immediately before the first protected request and verify isInterceptorPresent and isPinnerPresent. If either is false, call ApproovService.updateClientFactory(true) before proceeding.
  • iOS: fetch the metadata immediately after the first protected request and inspect sessionsWithoutPinning and unpinnedSessions. This helps detect delegate conflicts, skipped sessions, and missing pinning verification early in development and staging.

On Android, use logcat and filter with adb logcat | grep ApproovService. On iOS, use the Console app for a connected simulator or device and search for ApproovService.

Your Approov onboarding email should contain a link to Live Metrics Graphs. After you run your app with Approov integration you should see results in live metrics within a minute or so.

Next Steps

  • Read ARCHITECTURE.md for the service layer's network interception design on iOS and Android, race conditions, interference from third-party SDKs, and the fetchWithApproov alternative.
  • Read USAGE.md for detailed instructions on message signing, token binding, custom network mutators, API protection, and secrets protection.
  • Read REFERENCE.md for the complete React Native ApproovService interface.
  • Read TROUBLESHOOTING.md for platform-specific setup and runtime diagnostics.
  • See the Quickstart sample application for a working integration.

版本与发布记录

@approov/approov-service-react-native 共发布 19 个版本,首次发布于 2024/08/30;过去 12 个月发布了 16 个版本。

版本发布日期距今
3.5.18latest2026/10/061 个月前
3.5.172026/08/272 个月前
3.5.162026/08/142 个月前
3.5.152026/07/093 个月前
3.5.142026/06/104 个月前
3.5.132026/05/056 个月前

关于 @approov/approov-service-react-native 的常见问题

@approov/approov-service-react-native 是什么?

Approov Mobile App Protection for ReactNative

如何安装 @approov/approov-service-react-native?

在项目目录运行 npm install @approov/approov-service-react-native,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(>=19.0.0)、react-native(>=0.76.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@approov/approov-service-react-native 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@approov/approov-service-react-native 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@approov/approov-service-react-native 可以商用吗?

@approov/approov-service-react-native 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@approov/approov-service-react-native 的下载量有多少?

根据 npm 官方下载统计,@approov/approov-service-react-native 在 2026/10/07 – 2026/11/05 这 30 天内被下载 3,872 次,日均约 129 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@approov/approov-service-react-native 还在维护吗?

最新版本 3.5.18 发布于 2026/10/06(1 个月前);至今共发布 19 个版本,其中过去 12 个月发布了 16 个;首次发布于 2024/08/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。