我正在使用react-native的Textinput。如何在文本输入框中自动粘贴otp或代码。
最简单的方法是使用内置的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,则可以按照以下教程进行操作
希望这会有所帮助