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

查看:279
本文介绍了如何检测用户何时在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天全站免登陆