Is there any way to access data from iPhone inbox(SMS) to ios application to do automatic OTP verification like the one in Android? I shall be grateful for your help.
-
don't think so, you have to ask them to enter manually – Tj3n Sep 22 '16 at 05:59
12 Answers
In iOS 12 Apple has introduced feature called Security Code AutoFill
.
To use this in your app all you need to do is set UITextField
's input view’s textContentType
property oneTimeCode
.
otpTextField.textContentType = .oneTimeCode
NOTE: Security Code AutoFill will only works with System Keyboard it will not work with custom keyboard.
When you get OTP it will look something like this:

- 6,496
- 2
- 26
- 34
-
-
Yes, iphone 7 isn't asking me to save password or fill it when signing-in. I guess it needs some account saved in keychain before using it, – Waqas Nov 29 '18 at 07:25
-
@Waqas It will not ask you to save the password. When you get OTP you will see a toolbar above the keyboard with OPT code. – iVarun Nov 29 '18 at 07:27
-
1you are right that point is understood and well clear. I was asking about Autofill. In apple WWDC Session https://developer.apple.com/videos/play/wwdc2018/204/ It also talks about autofill passwords – Waqas Nov 29 '18 at 07:45
-
@iVarun It should, if you have iOS 12. I tried in iPhone SE, works super fine. – byJeevan Dec 08 '18 at 06:40
-
@byJeevan Yeah... It’s working fine. Waqas was looking for different answer – iVarun Dec 08 '18 at 06:45
-
**Also check SMS content to make sure that OTP number is Detected as OTP, NOT as phone Number** – Lal Krishna Sep 23 '19 at 06:36
-
is there specific format to identify one time code from message content? – Dhaval H. Nena Oct 10 '19 at 08:42
-
sorry i wanna ask is this .onetimecode can also works with whatsapp otp? or its just work with SMS otp? – afi permana Jul 12 '21 at 09:09
It is also important that the text message you receive contains something with "code" like
"your passcode is:123456"
or
"12345 is your code to log in"
something along that line.
NOT!
Your App: 12345
you can verify if the code in your text message will work with the .oneTimeCode type by tapping the underlined code in your message. If a dialog pops up that says "copy code", you are good to go. Otherwise you might need to change the text of your message.

- 993
- 6
- 19
UPDATE
From iOS 12 Apple will supports Password AutoFill on UITextField, UITextView, and any custom view that adopts the UITextInput protocol. System keyboard set the textContentType on it to .oneTimeCode
1) Using Code
singleFactorCodeTextField.textContentType = .oneTimeCode
2) Using Storyboard/XIB
Select
UITextField
/UITextView
instoryboard/XIB
click Click on Attribute inspector. Go to text input trait, click to Content type and select one time code and done.
The operating system will detect verification codes from Messages automatically with this UITextContentType
set.
Warning
If you use a custom input view for a security code input text field, iOS cannot display the necessary AutoFill UI.
For more information, you can check it on the Apple developer oneTimeCode
And also review WWDC 2018 Session 204 - Automatic Strong Passwords and Security Code AutoFill and jump to 24:28 for automatic pre-fill the OTP.

- 6,961
- 3
- 42
- 51
-
I am facing this problem https://stackoverflow.com/questions/61495065/how-to-enable-disable-onetimecode-feature-for-specific-text-field – Gaurav Borole Jul 07 '20 at 09:32
-
1what if i not clicked on "From Messages 180605" ? i want to fill textfiled without user click. – Nirav Kotecha Jan 27 '21 at 05:05
-
@iNiravKotecha I beleive it is not possible as how you will get the OTP data without click. – Ramkrishna Sharma Jan 27 '21 at 14:50
-
@RamkrishnaSharma i want like android. sms come and it will fill in textfield. – Nirav Kotecha Jan 28 '21 at 11:24
Currently for iOS 12 and above, you may use Security Code Autofill
oneTimeCodeTextField.textContentType =.oneTimeCode
However ApplePay is doing automatic verification since iOS 11 but that is not yet available to developers.
-
1is there any specific way to make this compatible with website otp's as well or does it just work by default? – Amien Jul 16 '18 at 16:16
-
-
It is not automatic right now as in android, but this is the best you can do. codeTextField.textContentType =.oneTimeCode – Ted Mar 07 '19 at 08:48
-
This seems to automatically input the OTP in the input field as well, while the other examples just show a suggestion on top of the keyboard. Is there any difference in the implementation between the two? – Umang Galaiya Mar 26 '19 at 20:31
-
This one is done by apple pay probably using private APIs. Not yet available for public as far as I know – Ted Mar 26 '19 at 21:22
-
-
@UmangGalaiya Can you specify that third party? Coz AFAIK, this only works when we tap on suggestion on top of the keyboard. I too haven't seen any third party automatically populating the textfield as soon as we get a message – Rajan Maheshwari Jun 15 '19 at 20:40
-
Can't name the third party, but they were definitely auto-reading the OTP and not relying on the suggestion that shows up on top of the keyboard. They've rolled back the feature since about two weeks after my first comment on this answer. – Umang Galaiya Jun 28 '19 at 08:50
Also...on the phone "Autofill Passwords" needs to be turned on.

- 1,807
- 14
- 23
-
2Sigh. This is the ONLY place I've seen to mention this. And I've been debugging and wondering why doesn't the code work... – JOM Mar 13 '20 at 11:21
-
1
-
@AntDC i am using iOS 13.4.1 keyboard is not showing auto fill suggestion, can you please help – Arshad Shaik Jun 12 '20 at 10:33
In Xamarin iOS, for >=iOS 12:
First of all, the SMS need to have the keyword "code" or "passcode" into their message, and don't use spaces after the code. if you received the SMS and you have the button "Copy Code" then it will works
Then you need to place this:
_txtField = new UITextField()
{
UserInteractionEnabled = true,
};
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
_txtField.TextContentType = UITextContentType.OneTimeCode;
}
_txtFieldDelegate = new UITextFieldDelegate();
_txtField.Delegate = _txtFieldDelegate;
_txtField.BecomeFirstResponder();
NOTE: Security Code AutoFill will only works with System Keyboard (not custom).

- 3,208
- 3
- 20
- 31
To get otp on your text field two things to be observed
1. set your input field as otp
Other answers let you know how manage your code to receive otp.
2. provide "code" phrase in your message
Your message must contains "code", "passcode", "password" in english and if you want to enable it in other language test with some word translated from "code" to your language
I have received message with bellow words:
Spanish: (Codingo)
Germany: (Kode)
Czech: (Kod)
Persian: (رمز)
Arabic: (رمز)
The otp number must be in english not your language number, your "Code" phrase must be separate with only one space with your numbers or with ":" without space.
Code 1111111
Code:111111
One other important thing you must be attention is about message preview. The numbers will be detected as otp if it has a gray line in below. if it shows in blue color or without line, Message does not notify you as otp. Somthing like image

- 988
- 2
- 12
- 30
I've got sollution from answer of santosh kumar and Ted
var otpText = String()
- in
viewDidload()
if #available(iOS 12.0, *) {
txtFirst.textContentType = .oneTimeCode
txtSecond.textContentType = .oneTimeCode
txtThird.textContentType = .oneTimeCode
txtForth.textContentType = .oneTimeCode
txtFifth.textContentType = .oneTimeCode
}
txtFirst.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
txtSecond.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
txtThird.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
txtForth.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
txtFifth.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .editingChanged)
txtFirst.becomeFirstResponder() //by doing this it will open the keyboard on first text field automatically
- Action for
TextField
//When changed value in textField
@objc func textFieldDidChange(textField: UITextField){
let text = textField.text
if text?.count == 1 {
switch textField{
case txtFirst:
txtSecond.becomeFirstResponder()
case txtSecond:
txtThird.becomeFirstResponder()
case txtThird:
txtForth.becomeFirstResponder()
case txtForth:
txtFifth.becomeFirstResponder()
case txtFifth:
txtFifth.resignFirstResponder()
self.dismissKeyboard()
default:
break
}
}
if text?.count == 0 {
switch textField{
case txtFirst:
txtFirst.becomeFirstResponder()
case txtSecond:
txtFirst.becomeFirstResponder()
case txtThird:
txtSecond.becomeFirstResponder()
case txtForth:
txtThird.becomeFirstResponder()
case txtFifth:
txtForth.becomeFirstResponder()
default:
break
}
}
else{
}
}
- OTP String and Dismiss KeyBoard
func dismissKeyboard(){
self.otpText = "\(self.txtFirst.text ?? "")\(self.txtSecond.text ?? "")\(self.txtThird.text ?? "")\(self.txtForth.text ?? "")\(self.txtFifth.text ?? "")"
print(self.otpText)
self.view.endEditing(true)
}
Most important thing: if you are using shouldChangeCharactersIn
method, please comment it. Else this code will not work

- 1,095
- 12
- 18

- 1,559
- 2
- 14
- 31
-
I am facing this problem https://stackoverflow.com/questions/61495065/how-to-enable-disable-onetimecode-feature-for-specific-text-field – Gaurav Borole Jul 07 '20 at 09:32
-
@GauravBorole if you facing that issue, then please set `textContentType` to `.oneTimeCode` only for OTP textField. – steveSarsawa Jul 07 '20 at 11:52
-
-
-
I get otP message text as "3125" only and message name comes from AD-DSHOTP – Dilip Tiwari Mar 19 '21 at 05:50
-
@DilipTiwari sorry but your msg must contain the "Code/CODE" or "Passcode/PASSCODE" keyword. Unless it won't be work. – steveSarsawa Mar 19 '21 at 06:41
-
1Hii @steveSarsawa by changing msg content as you suggested it resolved by issue thanks a lot i forgot to thank you. – Dilip Tiwari Apr 15 '21 at 07:32
-
You can get OTP from your message.
otptextField.textContentType = .oneTimeCode
Can please get the project from his link.

- 1,519
- 17
- 16
To support Autofill OTP Note : To read OTP from Messages, Message should contain code or passcode
if #available(iOS 12.0, *) {
optTextField.textContentType = .oneTimeCode
} else {
// Fallback on earlier versions
print("Fallback on earlier versions")
}

- 506
- 6
- 13
No.
Because this would be considered a privacy issue, you cannot access the users SMS inbox.

- 6,860
- 20
- 39
- 51

- 21
- 2
-
1Since you answered this question in 2017, there was no solution to the questioner issue but back in 2018 apple introduced Security Code AutoFill which helps reading code from SMS box. https://developer.apple.com/videos/play/wwdc2018/204/ – Mohammad Reza Koohkan Feb 21 '22 at 10:27