如何在消息键盘上显示验证码建议

How to show verification code suggestion on keyboard from Message

我看了这个视频 What's New in Cocoa Touch at WWDC 2018 并看到:

如何显示这些信息?

评论WWDC 2018 Session 204 - Automatic Strong Passwords and Security Code AutoFill

您需要使用 UITextField 输入和系统键盘(无自定义控件)并将其上的 textContentType 设置为 .oneTimeCode([=24= 中的新功能) ] 12).

let securityCodeTextField = UITextField()
securityCodeTextField.textContentType = .oneTimeCode

操作系统将自动检测消息中的验证码(包含 "code" 或 "passcode" 字样的消息)设置 UITextContentType

iOS 在 UITextField、UITextView 和任何采用 UITextInput 协议的自定义视图上支持密码自动填充。系统键盘将其上的textContentType设置为.oneTimeCode

singleFactorCodeTextField.textContentType = .oneTimeCode

Important

tvOS apps can also support Password AutoFill using the same content-type settings. The AutoFill QuickType bar appears above the keyboard when entering passwords with an iOS device using the Control Center keyboard, the Remote app, or the Continuity Keyboard. Focus is also advanced to the login button when the login fields are populated.

Warning

If you use a custom input view for a security code input text field, iOS cannot display the necessary AutoFill UI.

对于那些在 HTML 中搜索如何做到这一点的人:需要为您的输入字段添加 autocomplete="one-time-code"

<input id="single-factor-code-text-field" autocomplete="one-time-code"/>

(来自 Apple Docs

故事板

Select UITextField > 显示属性检查器 > 文本输入特征 > 内容类型 > 一次性代码