In Swift, I want to detect when the user has finished adding their username/password using autofill (from https://developer.apple.com/documentation/security/password_autofill ) and not reopen the keyboard, since they're presumably done using the keyboard.
Here's how I've setup their username/password inputs:
override func viewDidLoad() {
super.viewDidLoad()
name.textContentType = .username
password.textContentType = .password
loginButton.addTarget(self, action: #selector(self.loginPressed), for: .touchUpInside)
}
@objc func loginPressed() {
// Making server calls here
}
Right now, after I hit an autofill username, my iOS runs the FaceID check, then reopens the keyboard. The user has to manually close the keyboard after.
When a user uses autofill, depending on which type of view you use to get text input, a certain delegate method is called, as well as a textDidChangeNotification
( docs ). For UITextField
the method is ( textField(_:shouldChangeCharactersIn:replacementString:)
). The string passed in is whatever new string is added, and "normally contains only the single new character that was typed" but if the user uses autofill, or pastes in a password, the number of characters in the string will be longer. You could try checking for that and calling endEditing
if more than one new character is added.
override func viewDidLoad() {
super.viewDidLoad()
name.textContentType = .username
password.textContentType = .password
//set delegate
name.delegate = self
password.delegate = self
loginButton.addTarget(self, action:
#selector(self.loginPressed),
for: .touchUpInside)
}
@objc func loginPressed() {
// Making server calls here
}
extension UIViewController: UITextFieldDelegate
{
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
if textField == name
{
DispatchQueue.main.async {
self.view.endEditing(true)
}
}
return true
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.