Automatic OTP verification in iOS?

后端 未结 12 1223
遥遥无期
遥遥无期 2020-12-01 03:23

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.

相关标签:
12条回答
  • 2020-12-01 03:58

    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

    0 讨论(0)
  • 2020-12-01 03:58

    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")
            }
    
    0 讨论(0)
  • 2020-12-01 04:06

    You can get OTP from your message.

    otptextField.textContentType = .oneTimeCode
    

    Can please get the project from his link.

    https://github.com/karthickkck315/Automatic-OTP

    0 讨论(0)
  • 2020-12-01 04:07

    you can easily set this in Storyboard

    0 讨论(0)
  • 2020-12-01 04:08

    Also...on the phone "Autofill Passwords" needs to be turned on.

    0 讨论(0)
  • 2020-12-01 04: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.

    0 讨论(0)
提交回复
热议问题