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 username and password fields are UITextFields. I would like to identify when a user has "autofilled" one of the two UITextFields, so I can progress to the next step. Currently the user autofills an item, then needs to press the "Next" button on the on-screen keyboard in order to advance. I'd like to trigger this on behalf of the user.
The WWDC2017 Password Autofill session says to use UITextFieldTextDidChange. This works, but of course this is also triggered when a user is manually typing in those fields.
My thought has been to compare the prior version of the text with the new version of the text, and assume that if the length has increased from zero to greater than some minimal length (2 or more), the user used autofill. That should work most of the time, but has a risk of a false trigger (fast typing on slow device perhaps). So to me, this may be a risky assumption.
I'm curious is anyone has found a more surefire way to determine if Password Autofill has been used on a UITextField, or just thinks my worry about a false trigger is unfounded.
Not sure if the previous answer stopped working at some point, but I can't get it to work—I only get a single didBeginEditing
call when AutoFill is used.
However, I did find a way to detect AutoFill. And keep in mind that it is possible for AutoFill to be used after some characters have already been entered, for example if the user has already typed some numbers in the phone number, then they AutoFill the full number.
For Swift 4/5:
private var fieldPossibleAutofillReplacementAt: Date?
private var fieldPossibleAutofillReplacementRange: NSRange?
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
// To detect AutoFill, look for two quick replacements. The first replaces a range with a single space
// (or blank string starting with iOS 13.4).
// The next replaces the same range with the autofilled content.
if string == " " || string == "" {
self.fieldPossibleAutofillReplacementRange = range
self.fieldPossibleAutofillReplacementAt = Date()
} else {
if fieldPossibleAutofillReplacementRange == range, let replacedAt = self.fieldPossibleAutofillReplacementAt, Date().timeIntervalSince(replacedAt) < 0.1 {
DispatchQueue.main.async {
// Whatever you use to move forward.
self.moveForward()
}
}
self.fieldPossibleAutofillReplacementRange = nil
self.fieldPossibleAutofillReplacementAt = nil
}
return true
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With