How to auto fill OTP textfield on OTP code received from firebase in iOS, Swift? (WITHOUT TAPPING)
Asked Answered
Z

1

7

I want to know a callback function that is automatically called and fill the OTP text field (after I receive the OTP code from firebase).

I have already tried: textfield.contentType = onetimecode.

But this is not what I am looking for, this just gives the otp code fetched from sms as a autocomplete. I am actually looking for a callback function that is auto fired after I receive OTP code on my phone.

I think picture makes it clear, I want to fill the textfield without tapping (keyboard placeholder thingy) the received OTP code.

OTP screen

Zillah answered 17/2, 2022 at 6:8 Comment(3)
I don't think it's possible. It's Android feature, have you seen it in any iOS app?Compatible
Most probably No. But can you explain why this is not possible, is it because of sandbox or the functionality doesn’t exist yet? Reply in answer section if you know the reason.Zillah
@RehanAliKhan I do not think such a callback is supported in iOS. It is designed this way - needs explicit user action on the input view. Reference: developer.apple.com/documentation/security/password_autofill/…Tun
L
4

By design, iOS apps have no access to incoming SMS messages. There are many discussions out there on a similar topic.

The main reason for the restriction is privacy. There is no documented public API to receive a callback when SMS is received. If you try to hack iOS and listen for private notifications then might be your app will be rejected by the App Review team.

While doing research on this topic I found something interesting, you can create an extension that can receive incoming SMS and call events for the purpose of SPAM filtering. This type of extension is called the SMS and Call reporting extension in Apple's ecosystem. While looking at the documentation of this extension, at first glance I thought we can achieve the asked behavior if we can notify our host app about the incoming messages. But while scrolling to the end of the document I found that by design this extension has some additional restrictions:

  • The extension cannot share data with the host app.
  • The extension can't access the network directly.
  • The extension receives the event only if the sender's number is not on the contact list

additional restrictions

Anyways it's all about docs, I have not tried anything on the extension. What you can do is try to create an extension and see for other possible solutions.

Example:

1 bad solution in my mind is your extension call your server with the SMS content and then your server forward the SMS content in a silent push notification payload. When your app received the silent notification it serves the purpose of prefilling the field. But again this is not the intended purpose of the extension.

In my opinion, the best is what you have already achieved i.e. setting the text field's content type.

Ludivinaludlew answered 20/2, 2022 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.