如何检测用户何时在 UITextField 上使用密码自动填充 [英] How to detect when user used Password AutoFill on a UITextField

查看:43
本文介绍了如何检测用户何时在 UITextField 上使用密码自动填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了在 iOS 11 上支持密码自动填充所需的所有应用程序和服务器更改,并且运行良好.我希望它工作得更好一点.

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.

我的用户名和密码字段是 UITextFields.我想确定用户何时自动填充"了两个 UITextField 之一,以便我可以进行下一步.当前用户自动填充一个项目,然后需要按屏幕键盘上的下一步"按钮才能前进.我想代表用户触发此操作.

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.

WWDC2017 密码自动填充会话说要使用 UITextFieldTextDidChange.这是有效的,但当然这也会在用户手动输入这些字段时触发.

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.

我的想法是将文本的先前版本与文本的新版本进行比较,并假设如果长度从零增加到大于某个最小长度(2 或更多),则用户使用自动填充.这应该在大多数情况下都有效,但有误触发的风险(可能在慢速设备上快速打字).所以对我来说,这可能是一个冒险的假设.

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.

我很好奇是否有人找到了一种更可靠的方法来确定是否在 UITextField 上使用了密码自动填充,或者只是认为我对错误触发器的担心是没有根据的.

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.

推荐答案

不确定之前的答案是否在某个时候停止工作,但我无法让它工作 - 我只有一个 didBeginEditing在使用自动填充时调用.

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.

对于 Swift 4/5:

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
}

这篇关于如何检测用户何时在 UITextField 上使用密码自动填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆