如何自动将短信中收到的一次性代码粘贴到本机文本输入中

问题描述 投票:1回答:1

我正在使用react-native的Textinput。如何在文本输入框中自动粘贴otp或代码。

react-native textinput
1个回答
0
投票

最简单的方法是使用内置的SMS侦听包。

https://www.npmjs.com/package/react-native-android-sms-listener

https://www.npmjs.com/package/react-native-otp-verify

如果您使用react-native-android-SMS-listner包,则可以使用以下代码。

let subscription = SmsListener.addListener(message => {
  let verificationCodeRegex = /Your verification code: ([\d]{6})/

  if (verificationCodeRegex.test(message.body)) {
    let verificationCode = message.body.match(verificationCodeRegex)[1]

    YourPhoneVerificationApi.verifyPhoneNumber(
      message.originatingAddress,
      verificationCode
    ).then(verifiedSuccessfully => {
      if (verifiedSuccessfully) {
        subscription.remove()
        return
      }

      if (__DEV__) {
        console.info(
          'Failed to verify phone `%s` using code `%s`',
          message.originatingAddress,
          verificationCode
        )
      }
    })
  }
})

如果您使用react-native-otp-verify,则可以按照以下教程进行操作

https://tech.goibibo.com/building-otp-verification-component-in-react-native-with-auto-read-from-sms-2a9a400015b0

希望这会有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.