代码之家  ›  专栏  ›  技术社区  ›  Laffen

密码自动填充将密码插入父UIViewController

  •  0
  • Laffen  · 技术社区  · 3 年前

    用户可以使用密码自动填充设置新密码。密码也被添加到父视图控制器的文本字段中。

    查看控制器堆栈

    UINavigationController -> View Controller A -> View Controller B -> View Controller C
    视图控制器A有两个电子邮件/密码字段,类型为UITextField。

    问题

    当在View Controller C中重置密码时,View Controller弹出,我在delegate函数中观察到 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool 新生成的密码被插入视图控制器B的电子邮件字段和视图控制器A的电子邮件字段。导致输入字段的值类似于 bob@company.comNEWLY-MADE-PASSWORD . 为什么密码被插入UITextField中 .textContentType = .emailAddress 首先,我也很困惑。

    为什么密码自动填充试图 聪明的 并将新密码插入父视图控制器的字段?这对我来说有点头痛,导致在委托函数中进行一些不可靠的检查,以拒绝插入密码。

    针对El Tomato的评论: 苹果在其产品中使用“popped”一词 docs 描述你打电话时发生的事情 popViewController(animated:) .

    没有显示视图控制器。它们都被推到由 UINavigationController ,如标题中所述-> View controller stack .

    没有相关代码,密码自动填充是由开发者隐藏的启发式算法解决的,这是苹果公司的内置功能。

    解决方法

    我找到了一种解决方法,可以防止生成的密码被插入父视图控制器的UITextField中。希望这能帮助其他遇到同样问题的人。

    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 回复  |  直到 2 年前
    推荐文章