搜索插件、代码仓与 APP 分类

插件详情v4.1.0

otp_autofill

使用用户同意API和检索器API实现的Android OTP自动填充。添加从其他输入接收OTP代码的功能。

AndroidiOS
近 30 天下载
2.9万
喜欢数
293
Pub 分数
150

安装插件

flutter pub add otp_autofill

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

otp_autofill 包通过使用用户同意 API 和检索器 API,使 Android 的 OTP 自动填充成为可能,允许应用自动检测并填充来自短信的一次性密码。它支持从其他输入源接收 OTP 代码,从而在身份验证过程中提升用户体验。该包还适用于 iOS,为 PIN 和 OTP 字段提供跨平台兼容性。专为与表单输入无缝集成而设计,可减少手动输入错误并加快登录流程。

适合场景

  • 登录表单中的自动OTP检测
  • 增强双重身份验证期间的用户体验
  • 支持基于短信的验证流程
  • 与现有的 PIN 或密码输入字段集成
  • 从备用来源启用回退OTP检索

主要特点

  • 使用 Android 的用户同意和检索器 API
  • 跨平台支持(Android 和 iOS)
  • 从短信中自动提取OTP代码
  • 用于获取OTP的备用输入方法
  • 与文本字段的简单集成

适合对象

  • 需要安全OTP输入的应用
  • 带短信验证的身份验证流程
  • 寻求减少用户摩擦的开发者
  • 针对 Android 和 iOS 平台的项目
  • 包含 PIN/OTP 字段的表单密集型应用程序

使用建议

  • 需要 Android SDK 28+ 才能获得完整功能
  • 仅限 Android 短信自动填充,无需额外设置
  • iOS 实现依赖于系统级别的 OTP 处理
  • Android 上短信访问所需的权限
  • 可能需要在不同设备制造商之间进行测试

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

OTP autofill

Made by Surf 🏄‍♂️🏄‍♂️🏄‍♂️

Build Status Coverage Status Pub Version Pub Likes Pub popularity Flutter Platform

Overview

This plugin uses the SMS User Consent API and SMS Retriever API on Android.

You could use autofill from another input by using the OTPStrategy (e.g. from push-notification).

For testing you could create a TestStrategy.

iOS

On iOS, the OTP autofill feature is integrated into the TextField component. The code received from an SMS is stored for a duration of 3 minutes.

Rules for sms
  1. Sms must contain the word code or its translation in iOS supported localizations.
  2. The sms should contain only one sequence of digits.
iOS Testing

The iOS platform is capable of receiving OTP from any phone number, not just a specific sender.

Android

  • OTPInteractor.hint - displays a system dialog that allows the user to select from their saved phone numbers (recommendation from Google).
  • OTPInteractor.getAppSignature - creates the hash code of your application, which is used in the SMS Retriever API.
  • OTPInteractor.startListenUserConsent - the broadcast receiver that starts listening for OTP codes using the SMS User Consent API. It listens for a duration of 5 minutes, after which a timeout exception occurs.
  • OTPInteractor.startListenRetriever - the broadcast receiver that starts listening for OTP codes using the SMS Retriever API. It listens for a duration of 5 minutes, after which a timeout exception occurs.
  • OTPInteractor.stopListenForCode - used in dispose.

The plugin is designed to receive the full text of an SMS message and requires a parser to extract the relevant information from the message.

If you use the SMS User Consent API, the system will prompt the user for permission to access and read incoming messages.

Rules for SMS when using SMS User Consent API
  1. The message should contain an alphanumeric string of 4 to 10 characters, with at least one digit.
  2. The message was sent from a phone number that is not in the user's contacts.
  3. If the sender's phone number is specified, the message must originate from that number.
Rules for SMS when using SMS Retriever API
  1. The length should not exceed 140 bytes.
  2. It should contain a one-time code that the client sends back to your server to complete the verification process.
  3. It should include an 11-character hash string that identifies your app (refer to the documentation for server for more details). For testing, you can obtain it from OTPInteractor.getAppSignature.
Android Testing

The OTPInteractor.startListenForCode method allows the application to start receiving verification codes from a specific phone number, specified by the senderPhone argument.

You also can use Android Emulator to test the plugin. To do this, you need to send an SMS to the emulator with the following command:

adb emu sms send <senderPhone> <message>

E.g.:

adb emu sms send 900 "Your code is 12345. Do not share it with anyone."

Make sure senderPhone is the same as the one you specified in the startListenForCode method.

Usage

You should use OTPInteractor to interact with OTP.

To simplify implementation, consider using the OTPTextEditController as a controller for your TextField.

OTPTextEditController.startListenUserConsent - uses the SMS User Consent API and listens to user strategies. OTPTextEditController.startListenRetriever - uses the SMS Retriever API and listens to user strategies. OTPTextEditController.startListenOnlyStrategies - only listens to user strategies. OTPTextEditController.stopListen - used in dispose.

Installation

Add otp_autofill to your pubspec.yaml file:

dependencies:
  otp_autofill: $currentVersion$

At this moment, the current version of otp_autofill is otp_autofill version.

Android Installation

Set minSdkVersion at least to 19 in <project root>/android/app/build.gradle.

android {
  ...
  defaultConfig {
    ...
    minSdkVersion 19
    ...
  }
  ...
}

Example

  1. Create a simple strategy
class SampleStrategy extends OTPStrategy {
  @override
  Future<String> listenForCode() {
    return Future.delayed(
      const Duration(seconds: 4),
      () => 'Your code is 54321',
    );
  }
}
  1. Initialize and set the listener
late OTPTextEditController controller;
final scaffoldKey = GlobalKey();

@override
void initState() {
  super.initState();
  _otpInteractor = OTPInteractor();
  _otpInteractor.getAppSignature()
      .then((value) => print('signature - $value'));
  controller = OTPTextEditController(
    codeLength: 5,
    onCodeReceive: (code) => print('Your Application receive code - $code'),
  )..startListenUserConsent(
      (code) {
        final exp = RegExp(r'(\d{5})');
        return exp.stringMatch(code ?? '') ?? '';
      },
      strategies: [
        SampleStrategy(),
      ],
    );
}

Send new code

To receive a new code when a timeout exception occurs, you can pass a callback function to the onTimeOutException parameter and start listen for a new code.

controller = OTPTextEditController(
      codeLength: 5,
      onCodeReceive: (code) => print('Your Application receive code - $code'),
      otpInteractor: _otpInteractor,
      onTimeOutException: () {
        //TODO: start new listen to get new code
        controller.startListenUserConsent(
          (code) {
            final exp = RegExp(r'(\d{5})');
            return exp.stringMatch(code ?? '') ?? '';
          },
          strategies: [
            SampleStrategy(),
          ],
        );
      },
    )..startListenUserConsent(
        (code) {
          final exp = RegExp(r'(\d{5})');
          return exp.stringMatch(code ?? '') ?? '';
        },
        strategies: [
          TimeoutStrategy(),
        ],
      );

Changelog

All significant changes to this project will be documented in this file.

Issues

To report any issues, submit them directly in the Issues section.

Contribute

If you wish to contribute to the package (for instance, by enhancing the documentation, fixing a bug, or introducing a new feature), please review our contribution guide first and then submit your pull request.

Your PRs are always welcome.

How to reach us

Please don't hesitate to ask any questions about this package. Join our community chat on Telegram. We communicate in both English and Russian.

Telegram

License

Apache License, Version 2.0

所属分类

源码仓库

使用此插件的项目

暂无相关数据。