OTP autofill on Android and iOS: a reference for every platform
OTP autofill on Android and iOS works in four ways: iOS and the web read the code with the one-time-code field type, Android apps use the SMS User Consent or SMS Retriever API, and websites on Android can use WebOTP. On DiOTP's shared template, iOS, web one-time-code and Android User Consent all work. Silent Android autofill (Retriever) and WebOTP need a template of your own, because they need extra text in the message.
By Vivek Singh, Founder, Diorbit TechnologiesUpdated Last checked
How does OTP autofill work on Android and iOS?
Every autofill method reads the code from the SMS your user receives. Some read any message that contains a code; others only fill if the message carries a marker that ties it to your app or your website. That difference decides what works when you send OTP without DLT on a shared template, where the wording is fixed and you cannot add a marker.
On DiOTP the shared template reads: "{code} is your login OTP for {company}. Do not share it with anyone. Powered by Diorbit Technologies!" Only the code and your company name change. It has no app hash and no domain line.
| Method | What the SMS must contain | Shared template | Your own template |
|---|---|---|---|
| iOS app (oneTimeCode) | Any SMS that contains a code | Yes | Yes |
| Web form (autocomplete="one-time-code") | Any SMS that contains a code (Safari on iOS) | Yes | Yes |
| Android app: SMS User Consent API | Any SMS with a 4 to 10 character code; user taps Allow | Yes | Yes |
| Android app: SMS Retriever API | Your app's 11-character hash in the SMS | No | Yes, with the hash in your template |
| Website: WebOTP API | A last line "@yourdomain #code" | No | Yes, with that line in your template |
This page is the reference for what works where and how to fix it. If you are building a native or Flutter app, the Android and Flutter build guide has the backend pattern and full Kotlin and Dart code.
iOS: the oneTimeCode field type
iOS reads incoming SMS for anything that looks like a verification code and offers it in the QuickType bar above the keyboard. Your app only has to mark the field. There is no permission, no app hash and no special format, so it works with the shared template as it is.
Swift: mark the field as a one-time code
// UIKit
otpField.textContentType = .oneTimeCode
otpField.keyboardType = .numberPad
// SwiftUI
TextField("Enter the code", text: $code)
.textContentType(.oneTimeCode)
.keyboardType(.numberPad)In Flutter, the same hint is AutofillHints.oneTimeCode on the TextField. In React Native, set textContentType="oneTimeCode" on the TextInput. The user still taps the suggestion; iOS does not fill the field on its own.
Web: autocomplete="one-time-code" and WebOTP
autocomplete="one-time-code"
On a website, one attribute does for Safari on iPhone what oneTimeCode does for apps. It is harmless everywhere else, so put it on every OTP field.
HTML: an OTP field that autofills
<!-- Safari on iPhone and many Android keyboards offer the code above the keyboard -->
<label for="otp">Enter the code we sent</label>
<input id="otp" name="otp" type="text"
inputmode="numeric" autocomplete="one-time-code"
maxlength="10">WebOTP
The WebOTP API lets a website on Android, in Chrome and other browsers that support it, read the code from an SMS after one tap. It is stricter than the attribute above: the message must end with a line that names your website's domain and repeats the code, in the form "@yourdomain.in #123456". The browser checks that the domain matches the page before it hands over the code.
JavaScript: WebOTP, with a fallback to typing
// WebOTP: only fills if the SMS ends with a line like "@yourdomain.in #123456"
if ('OTPCredential' in window) {
const ac = new AbortController();
document.querySelector('form').addEventListener('submit', () => ac.abort());
navigator.credentials
.get({ otp: { transport: ['sms'] }, signal: ac.signal })
.then((otp) => { document.querySelector('#otp').value = otp.code; })
.catch(() => { /* no matching SMS: the user types the code */ });
}The shared template does not end with an "@domain #code" line, and its wording cannot be changed, so WebOTP never fires on it. If you want WebOTP, register your own OTP template with that last line. Keep autocomplete="one-time-code" on the field either way, so iPhone users still get the suggestion.
Android: SMS User Consent or SMS Retriever?
Native Android apps have two Google Play services APIs that read an OTP without the READ_SMS permission. Which one you can use depends on your template.
SMS User Consent API: works on the shared template
Your app starts listening just before it requests the code. When an SMS with a 4 to 10 character code arrives within five minutes, Android shows the user the message and asks whether to share it with your app. One tap and the code fills. It needs no hash, so the shared template works. It ignores messages from senders in the user's contacts, which is not a problem for a DLT header such as VM-DIORBT.
SMS Retriever API: needs your own template
The Retriever reads the message silently, with no prompt, but only if the SMS contains an 11-character hash computed from your app's package name and signing certificate. That hash is how Android knows the message is meant for your app. The shared template has no place for it, so Retriever autofill needs your own DLT template with the hash written in.
The React Native SMS Retriever guide covers the Retriever from React Native. The Android and Flutter guide has a complete Kotlin User Consent example.
What does your own template need for full autofill?
If silent Android autofill or WebOTP matters to you, register your own OTP content template on DLT with the extra text built in. The app hash and your domain never change, so they can be fixed text in the template, and only the code is a {#var#} variable. Examples:
Example DLT templates for autofill
Retriever (Android app), your own DLT template, sender = your header:
{#var#} is your login code for Acme. Do not share it with anyone. FA+9qCX9VSu
WebOTP (website), your own DLT template, last line bound to your domain:
{#var#} is your login code for Acme. Do not share it with anyone.
@acme.in #{#var#}- Compute the hash from the signing key your release build is signed with. If Google Play signs your app, use the Play signing key, not your upload or debug key, or the hash will not match on users' phones.
- If you have debug and release builds, they have different hashes. Test with the release hash before you register the template.
- For WebOTP, the "@domain #code" line must be the last line, and the domain must be the one your OTP page is served from.
- Check that your DLT platform accepts the line break and the hash characters in the template before you submit it; the DLT template checker helps with the wording.
Once your template, header and entity are approved, add them under Sender IDs in the DiOTP dashboard. Your send and verify calls do not change. Until then, keep sending on the shared template with User Consent and one-time-code autofill.
Why is OTP autofill not working? A troubleshooting list
Nothing fills on any platform
First check that the SMS arrived at all. If it did not, autofill is not the problem; see why an OTP is not received in India.
iOS or Safari
- The field is missing textContentType = .oneTimeCode or autocomplete="one-time-code".
- You split the code into one box per digit. Autofill targets one field; use a single field and style it, or make sure the first box carries the hint and spreads the pasted value.
- The field was not focused when the SMS arrived. The suggestion appears in the keyboard bar of the focused field.
Android User Consent
- You started listening after the SMS arrived. Call startSmsUserConsent before your backend sends the code.
- More than five minutes passed, so the listener timed out. Start it again on every resend.
- The user tapped Deny. Leave the field editable so they can type the code.
- Your digit regex is too strict. On the shared template the code is the first run of digits in the message.
Android Retriever
- You are on the shared template. The Retriever cannot work there; use User Consent.
- The hash in the template is from a different signing key than the installed build.
- The message is sent from a different template than the one with the hash.
WebOTP
- The page is not served over HTTPS, or the domain in the last line does not match the page.
- The "@domain #code" line is not the last line of the message, or the template is the shared one.
- The browser does not support WebOTP. Keep autocomplete="one-time-code" as the fallback.
Sources
Questions and answers
Does OTP autofill work without DLT registration?
Partly. On DiOTP's shared template, iOS one-time-code autofill, the web one-time-code attribute and the Android SMS User Consent API all work. Silent Android autofill and WebOTP need your own template.
Why does WebOTP not work with the shared template?
WebOTP needs the SMS to end with a line naming your domain and the code, such as @yourdomain.in #123456. The shared template has fixed wording without that line.
Do I need an app hash for iOS autofill?
No. App hashes are an Android SMS Retriever feature. iOS reads the code from any SMS that contains it once the field is marked as a one-time code.
What is the difference between SMS User Consent and SMS Retriever?
User Consent shows the message and asks the user to allow sharing it, and needs no hash. Retriever reads the message silently but only if it contains your app's 11-character hash.
Will my autofill break when I move to my own sender ID?
No. iOS, web one-time-code and User Consent keep working on any template. Moving lets you add Retriever or WebOTP if your new template includes the hash or domain line.
Why does autofill fill the wrong number?
Usually a regex picks up other digits in the message. On the shared template the code comes first, so take the first run of digits.
