Password AutoFill inserts password into parent UIViewController

483 Views Asked by At

I am using a UINavigationController to push multiple view controllers. At one point, the user is able to use Password AutoFill for setting a new password. The password is also being added to textfields for parent view controllers.

View controller stack

UINavigationController -> View Controller A -> View Controller B -> View Controller C
View Controller A has two fields for email/password, of type UITextField.
View Controller B has one field for email, of type UITextField.
View Controller C has two fields for password/confirmPassword, of type UITextField.

The problem

When the password is reset in View Controller C, the View Controller is popped and I observe in the delegate function func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool that the newly made password is being inserted into the email field for View Controller B AND the email field for View Controller A. Resulting in the input fields having a value something similar to [email protected]. Why the password is being inserted into a UITextField with .textContentType = .emailAddress in the first place, is also confusing to me.

Why is Password AutoFill attempting to be smart and insert the new password into the field of the parent view controllers? This is somewhat of a headache for me, resulting in some wonky checks in the delegate function to deny the insertion of a password.

In response to the comment made by El Tomato: Apple uses the term "popped" in their docs to describe what happends when you call popViewController(animated:).

There are no view controllers presented. They are all pushed to the stack managed by the UINavigationController, as stated in the header -> View controller stack.

There is no relevant code to present, Password AutoFill is resolved by heuristics hidden from the developer, this is a built-in feature made by Apple.

Workaround

I found a workaround to prevent the generated password from being inserted into UITextField of parent view controller. Hopefully this will help others who are experiencing the same problem.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if string.split(separator: "-").count == 3 {
        // A password autofill triggered from ResetPasswordViewController, this seems like a bug and I could not find any documentation for this.
        return false
    }
    return true
}
0

There are 0 best solutions below