@eabdullazyanov/react-native-sms-user-consent

中文文档
1.3.0发布于 2025/11/17MITTypeScriptCommonJS零依赖近 30 天下载 4.5万

React Native wrapper for Android's SMS User Consent API, ready to use in React Native apps with minimum effort

原始使用文档
文档目录10 节
  1. iOS
  2. Getting started
  3. Basic usage
  4. Customise verification code parser
  5. API
  6. useSmsUserConsent()
  7. startSmsHandling()
  8. retrieveVerificationCode()
  9. Help
  10. Contribution

React Native SMS User Consent

React Native wrapper for Android's SMS User Consent API, ready to use in React Native apps with minimum effort. The purpose of SMS User Consent API is to provide one-tap auto-filling of SMS verification codes.

iOS

SMS User Consent API exists only on Android, so this package is Android-only. Calling this package's APIs on iOS is no-op.

If you want auto-filling on iOS, textContentType="oneTimeCode" for TextInput is the way to go. Basically, this is the only way for iOS.

Getting started

Install the package:

yarn add @eabdullazyanov/react-native-sms-user-consent

or

npm install @eabdullazyanov/react-native-sms-user-consent

Basic usage

import React, { useEffect, useState } from 'react';
import { TextInput } from 'react-native';

import { useSmsUserConsent } from '@eabdullazyanov/react-native-sms-user-consent';

const Example = () => {
  const [code, setCode] = useState();

  const retrievedCode = useSmsUserConsent();

  useEffect(() => {
    if (retrievedCode) setCode(retrievedCode);
  }, [retrievedCode]);

  return <TextInput value={code} onChangeText={setCode} />;
};

In the example we use a controlled TextInput for the code entry. retrievedCode equals to the empty string initially, and whenever an SMS is handled retrievedCode receives the code from it. We use the useEffect to update the input value when an SMS is handled.

Customise verification code parser

Set the length of the validation code:

const retrievedCode = useSmsUserConsent(4);
// e.g. code = 1234

Provide a custom regular expression:

const retrievedCode = useSmsUserConsent('[A-Z0-9]{5}');
// e.g. code = A12B3

API

useSmsUserConsent()

useSmsUserConsent(config: number | string): string

React hook that starts SMS handling and provides the received code as its return value, which is the empty string initially. Stops handling SMS messages on unmount. Uses startSmsHandling and retrieveVerificationCode internally.

This hook is the way to go in most cases. Alternatively, you can use startSmsHandling and retrieveVerificationCode directly if dealing with something that is not a functional component or you need some more flexibility.

On iOS it just returns the empty string, so no additional code to handle iOS is needed.

Accepts an optional config parameter. In case if it's a number, it defines the length of the code. In case if it is a string, it defines a custom RegExp to parse the code.

startSmsHandling()

startSmsHandling(onSmsReceived: (event: {sms?: string}) => void): (
  stopSmsHandling(): void
)

Starts the native SMS listener that will show the SMS User Consent system prompt. If the user allowed reading the SMS, then the onSmsReceived callback is called. onSmsReceived receives the event object containing the SMS.

Returns stopSmsHandling function that stops showing the system prompt and stops SMS handling.

retrieveVerificationCode()

retrieveVerificationCode(sms: string, config: number | string): string | null

Retrieves the verification code from an SMS if there is any.

Accepts an optional config parameter. In case if it's a number, it defines the length of the code. In case if it is a string, it defines a custom RegExp to parse the code.


You can import the whole API as one object if you prefer

import SmsUserConsent from 'react-native-sms-user-consent';

// ...
SmsUserConsent.useSmsUserConsent();
// ...

Help

If you have any ideas about the project or found a bug or have a question, feel free to create an issue with all the relevant information. We are engaged to response ASAP. The following info will make it faster to resolve issues:

  1. Device or emulator model
  2. Android version
  3. Your environment info - output of the npx react-native info command

Contribution

PRs are always welcome. If you're feeling like contributing to the project, please do. It would be great to have all the relevant information with the PR.

To make changes, you'll need to follow these steps:

  1. clone the repo
  2. make changes
  3. test the changes
  4. create a PR
  5. 🥳

版本与发布记录

@eabdullazyanov/react-native-sms-user-consent 共发布 15 个版本,首次发布于 2021/03/02;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.3.0latest2025/11/1711 个月前
1.2.12024/11/212 年前
1.2.02024/04/262 年前
1.1.02024/02/212 年前
1.0.112023/03/253 年前
1.0.102021/09/065 年前

关于 @eabdullazyanov/react-native-sms-user-consent 的常见问题

@eabdullazyanov/react-native-sms-user-consent 是什么?

React Native wrapper for Android's SMS User Consent API, ready to use in React Native apps with minimum effort

如何安装 @eabdullazyanov/react-native-sms-user-consent?

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

@eabdullazyanov/react-native-sms-user-consent 支持 TypeScript 吗?

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

@eabdullazyanov/react-native-sms-user-consent 支持 ESM 还是 CommonJS?

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

@eabdullazyanov/react-native-sms-user-consent 可以商用吗?

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

@eabdullazyanov/react-native-sms-user-consent 的下载量有多少?

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

@eabdullazyanov/react-native-sms-user-consent 还在维护吗?

最新版本 1.3.0 发布于 2025/11/17(11 个月前);至今共发布 15 个版本,其中过去 12 个月发布了 1 个;首次发布于 2021/03/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。