概述
本文將介紹WWDC18 Automatic Strong Passwords and Security Code Autofill和WWDC17 Introducing Password AutoFill for Apps中提到的幾個功能: 密碼自動填充/自動生成的強密碼/驗證碼自動填充等.
如果需要更好的實現此功能,網站需要
支持HTTPS
,開發Demo可以用GitHub Pages.
在iOS11中,Apple已經做了啟發式功能,可以讓開發者在無感知的情況下,支持密碼的快速填充,但是為了更好的用戶體驗和降低未來版本中可能出現的bug,建議對不同的功能做一些額外的步驟配置. 默認效果是這樣的,QuickType Bar上沒有關聯域的賬戶密碼可選
功能一: iOS11密碼自動填充功能,賬戶密碼在QuickBar中自動顯示;iOS12自動保存密碼到iCloud鑰匙串
Infer login scenario
Check eligibility based on associated domains Find user name and password fields
Detect sign-in action
Prompt to save or update password
效果圖
工作原理
- iOS會自動推斷登錄場景
- 檢查關聯的域是否有效合格,從iCloud 鑰匙串中篩選出符合的賬戶和密碼顯示在QuickType Bar
- 根據設定輸入框的textContentType類別,找到用戶名和密碼輸入框,在用戶點擊QuickType Bar上的賬戶密碼是填充到對應位置
- 檢測登錄操作,在登錄頁面要從視圖層次結構中移除的時候,提示是否保存/更新賬戶密碼
開發人員需要配置的步驟
Associated Domains
設置UITextField的textContentType為對應類型
1.在蘋果開發者后臺開啟APP的Associated Domains服務
2.記錄Prefix和app的BundleID,生成apple-app-site-association文件,放到關聯域網站服務器的.well-known目錄下面或者根目錄下 . 注意:
網站需要支持HTTPS,Demo中為了演示,可以把項目和個人的Github Pages關聯,文件放到自己的Github Pages.
{
"webcredentials":{
"apps":["XW5558TH45.com.beike.testapp"]
}
}
官方示例存放目錄:
3.Xcode中項目配置,開啟Domains,點擊"+"添加一項, webcredentials:后面的"coderxllau.github.io"改為自己關聯的域,例如webcredentials:www.baidu.com等
4.Xcode中除了開啟并關聯域,還需要更改輸入框textContentType為指定類別
iOS 11和iOS 12中分別新增了新的Type.
UIKIT_EXTERN UITextContentType const UITextContentTypeUsername NS_AVAILABLE_IOS(11_0);
UIKIT_EXTERN UITextContentType const UITextContentTypePassword NS_AVAILABLE_IOS(11_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeNewPassword NS_AVAILABLE_IOS(12_0);
UIKIT_EXTERN UITextContentType const UITextContentTypeOneTimeCode NS_AVAILABLE_IOS(12_0);
self.userNameField.textContentType = UITextContentTypeUsername;
self.passwordField.textContentType = UITextContentTypePassword;
功能二: iOS12 自動生成建議的用戶和強密碼
效果圖
工作原理/設置步驟
和功能一Password Autofill基本一樣,不同的地方是在iOS 12中新增UITextContentTypeNewPassword,用這個字段標記新密碼輸入框即可在用戶點擊的時候自動生成強密碼填充
self.nameField.textContentType = UITextContentTypeUsername;
if (@available(iOS 12.0, *)) {
self.passwordField.textContentType = UITextContentTypeNewPassword;
self.passwordField.passwordRules = [UITextInputPasswordRules passwordRulesWithDescriptor:@"required: lower; required: upper; allowe: digit; required: [-]; minlength: 5;"];
} else {
self.passwordField.textContentType = UITextContentTypePassword;
}
密碼的格式
自動生成的密碼,我們可以通過一些方法指定生成的密碼格式和規則. 可以在官方提供的密碼格式工具Password Rules Validation Tool
上面調試自己的密碼格式,將生成的密碼格式描述復制下來,設置給UITextField的passwordRules屬性.
if (@available(iOS 12.0, *)) {
self.passwordField.textContentType = UITextContentTypeNewPassword;
self.passwordField.passwordRules = [UITextInputPasswordRules passwordRulesWithDescriptor:@"required: lower; required: upper; allowe: digit; required: [-]; minlength: 5;"];
}
功能三: iOS12 自動填充驗證碼
效果圖 , 從短信中獲取到驗證碼后自動顯示到QuickType Bar上
工作原理
在輸入框成慰第一響應者的時候,使用數據檢測器啟發式來推斷傳入消息攜帶安全代碼,把檢測到驗證碼顯示到QuickType Bar上,省去用戶的操作成本
缺陷:在demo中可以看到,自如發來的驗證碼也可以被demo獲取,對于驗證碼來源沒有做到很好的隔離
開發步驟:
開發步驟和功能一基本一致,在完成功能一的基礎上面,設置輸入框的輸入類別為UITextContentTypeOneTimeCode即可.
注意點
- 您應避免在視圖層次結構中構建定制鍵盤UI,或在控件中設置自定義輸入視圖。因為如果您這樣做,則會阻止iOS顯示必要的UI或注入相應的事件以代表您的用戶鍵入代碼。
- 關聯的網站要支持HTTPS
- 標記textContentType的輸入框獨立開,不要用一個輸入框標記兩個類別
- 驗證碼來源沒有做到很好的隔離
參考資料
WWDC18 Automatic Strong Passwords and Security Code Autofill
WWDC17 Introducing Password AutoFill for Apps;