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.
I've got sollution from answer of santosh kumar and Ted
var otpText = String()
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
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{
}
}
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
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")
}
You can get OTP from your message.
otptextField.textContentType = .oneTimeCode
Can please get the project from his link.
https://github.com/karthickkck315/Automatic-OTP
you can easily set this in Storyboard
Also...on the phone "Autofill Passwords" needs to be turned on.
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.