“您想保存此密码吗”对话框阻止键盘出现

Lif*_*lus 7 iphone ios swift

我对“您想保存此密码吗”对话框有疑问。当它弹出并且用户转到主屏幕并返回应用程序时,对话框消失并且他无法在触摸文本字段时抬起键盘。它在 iOS 13 上唯一像这样“工作”。在 iOS 12 上它工作正常,因为当用户返回应用程序时,对话框仍然存在。然后他可以保存密码或现在点击不并开始输入。任何想法如何解决这个问题?它可能是某种 iOS 13 错误。

Ste*_*cht 4

问题

正如OP所写:如果您启用了关联域,则在iOS 13中使用自动填充功能,然后您会得到一个UIAlertController,它要求您保存或更新密码,请参阅https://developer.apple.com/videos/play/wwdc2017 /206/了解更多信息。

iOS 13 的问题是,如果用户在点击“更新密码”或“现在不”按钮之前将应用程序置于后台,则切换回前台后,文本字段的键盘将不再显示,请参阅此处:

键盘不再显示

由于它是操作系统的系统对话框,因此您无法在进入后台之前以编程方式将其关闭。

因此,在苹果解决这个问题之前,人们可以:

  • 使用 SecRequestSharedWebCredential / SecAddSharedWebCredential 的解决方法
  • 或者不使用该功能
  • 或忽略边缘情况

解决方法

解决方法可能是:

  • 不要在 iOS 13 上使用新的自动填充功能
  • 使用 SecRequestSharedWebCredential / SecAddSharedWebCredential 代替

那么它可能看起来像这样:

使用SharedWebCredential

不要使用自动填充

为了不使用新的自动填充,不应设置textContentType ,因此不:

userTextField.textContentType = .username
passwordTextField.textContentType = .password
Run Code Online (Sandbox Code Playgroud)

也不要将 isSecureTextEntry 设置为 true。这意味着在实践中您需要自己的机制来隐藏密码文本字段的条目。有关建议,请参阅iOS 11 禁用密码自动填充附件视图选项?

SecRequestSharedWebCredential

在登录页面,可以在 viewDidLoad 中使用:

if #available(iOS 13, *) {
    requestCredentials()
} else {
    userTextField.textContentType = .username
    passwordTextField.textContentType = .password
}

private func requestCredentials() {
    SecRequestSharedWebCredential("software7.com" as CFString, nil, {
        credentials, error -> Void in
        guard error == nil else { return }
        guard let credentials = credentials, CFArrayGetCount(credentials) > 0 else { return }
        let unsafeCredential = CFArrayGetValueAtIndex(credentials, 0)
        let credential: CFDictionary = unsafeBitCast(unsafeCredential, to: CFDictionary.self)
        let dict: Dictionary<String, String> = credential as! Dictionary<String, String>
        let username = dict[kSecAttrAccount as String]
        let password = dict[kSecSharedPassword as String]

        DispatchQueue.main.async {
            self.userTextField.text = username;
            self.passwordTextField.text = password;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

SecAddSharedWebCredential

在第二个 ViewController 的 viewDidLoad 中可以使用:

if #available(iOS 13, *) {
    updateCredentials()
} else {
    //works automatically with autofill
}

private func updateCredentials() {
    SecAddSharedWebCredential("software7.com" as NSString as CFString,
                              self.userName as NSString as CFString,
                              self.password as NSString as CFString,
                              { error in if let error = error { print("error: \(error)") }
    })
}
Run Code Online (Sandbox Code Playgroud)

这看起来不像 iOS 13 的自动填充功能那么好,但它们允许您在用户进入 bg/fg 时继续使用键盘,仍然提供自动填充和共享凭据。一旦修复了错误,就可以删除此解决方法。