How to detect when user used Password AutoFill on a UITextField

前端 未结 7 2180
半阙折子戏
半阙折子戏 2020-12-10 03:26

I\'ve implemented all the app and server changes necessary to support Password Autofill on iOS 11, and it works well. I\'d like it to work a little better.

My usern

7条回答
  •  旧时难觅i
    2020-12-10 04:17

    Found a solution.

    When the password manager is used to autofill username + password, it will trigger didBeginEditing twice, faster than a human ever could.

    So, I calculate the time between the events. If the time is extremely fast, then I assume that autofill (e.g. FaceID or TouchID) was used to enter credentials and auto-trigger whatever UI is next -- in my case, the User tapping "Sign-in".

    Obviously, you have to set up the correct delegation of the UITextFields you want to monitor, but once you do that:

    var biometricAutofillTime: Date!
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        if biometricAutofillTime != nil {
            if Date().timeIntervalSince(biometricAutofillTime) < 0.1 {
                // NOTE: Need to hesitate for a very short amount of time,
                //        because, otherwise, the second UITextField (password)
                //        won't yet be populated
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { self.didTapSignin() }
            }
            biometricAutofillTime = nil
        }
        biometricAutofillTime = Date()
    }
    

提交回复
热议问题