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.
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:
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.
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.
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.
Also...on the phone "Autofill Passwords" needs to be turned on.
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
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).
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
textContentType
to .oneTimeCode
only for OTP textField. –
Doersten You can get OTP from your message.
otptextField.textContentType = .oneTimeCode
Can please get the project from his link.
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")
}
No.
Because this would be considered a privacy issue, you cannot access the users SMS inbox.
© 2022 - 2024 — McMap. All rights reserved.