前言 Password AutoFill 是iOS11及之后蘋果官方推出的新功能,可用于注冊的時候自動生成密碼,登錄成功之后保存帳號、密碼,相關帳號密碼會保存在iCloud的KeyChain中,下次登錄的時候,可以填充已存儲的帳號密碼,可以自動填充短信驗證碼。 可以簡化使用App 和 網頁登錄和啟動流程。原生端的UITextField和UITextView都可以做相關配置。我們在Safari瀏覽器中登錄成功后保存的密碼,在App中,也可以自動填充帳號密碼。
一、Password AutoFill準備工作
項目配置
Xcode配置
TARGETS -> Signing & Capabilities -> Associated Domains:添加如:webcredentials:你的域名
服務端配置
在服務端這個路徑下添加apple-app-site-association的文件,文件內容為如下json內容。
{
"webcredentials": {
"apps": ["teamID.bundleID"]
}
}
檢驗方式
簡單校驗方式:訪問 https://你的域名/.well-known/apple-app-site-association 可以訪問到相應文件。
二、注冊提示、填充強密碼
UITextField或UITextView 配置用戶名、密碼文本內容類型。
如果我們想要用戶在注冊的時候,給用戶一個自動生成密碼的選擇。可以使用 passwordTextField.textContentType = UITextContentTypeNewPassword;
指定自動填充密碼。
效果圖
代碼
accountTextField.textContentType = UITextContentTypeUsername;
accountTextField.keyboardType = UIKeyboardTypeEmailAddress;
passwordTextField.textContentType = UITextContentTypeNewPassword;
passwordTextField.secureTextEntry = YES;
UITextContentTypeUsername
及 UITextContentTypeNewPassword
適用于系統版本大于等于 iOS11的設備。
UIKIT_EXTERN UITextContentType const UITextContentTypeUsername API_AVAILABLE(ios(11.0));
UIKIT_EXTERN UITextContentType const UITextContentTypePassword API_AVAILABLE(ios(11.0));
我們可以對密碼填充規則有我們自己的規則,可以使用 UITextInputPasswordRules
配置密碼填充規則。
// 配置密碼規則
passwordTextField.passwordRules = [UITextInputPasswordRules passwordRulesWithDescriptor:@"required: lower; required: upper; required: digit; required: [-]; required: [&]; required: [+]; minlength: 20;"];
上述密碼輸入框的密碼填充規則為,生成的密碼需要滿足含小寫、大寫字母,數字、 -、&、+的條件,且最小長度為20位。更多相關內容可查看密碼驗證工具。
UITextInputPasswordRules
適用于系統版本大于等于 iOS12的設備。
UIKIT_EXTERN API_AVAILABLE(ios(12.0)) @interface UITextInputPasswordRules : NSObject <NSSecureCoding, NSCopying>
遇到問題
問題1:iCloud Keychain is disabled
[AutoFill] Cannot show Automatic Strong Passwords for app bundleID: bundleID內容 due to error: iCloud Keychain is disabled
上述報錯是因為iCloud 的 Keychain沒有打開。
處理方式為:設置 iCloud 鑰匙串 設置 -> 點擊名字 -> 點擊iCloud -> 點擊鑰匙串 -> 打開。
三、登錄提示、填充已存儲帳號密碼
當我們想要保存帳號、密碼的時候需要配置。帳號、密碼輸入框相應的textContentType。
效果圖
鍵盤上方的QuickTypeBar 顯示小鑰匙
顯示已經存儲過的帳號密碼
登錄成功后系統自動存儲密碼
修改已存登錄帳號密碼
代碼
accountTextField.textContentType = UITextContentTypeUsername;
accountTextField.keyboardType = UIKeyboardTypeEmailAddress;
passwordTextField.textContentType = UITextContentTypePassword;
passwordTextField.secureTextEntry = YES;
UITextContentTypeUsername
、UITextContentTypePassword
適用于系統版本大于等于 iOS11的設備。
UIKIT_EXTERN UITextContentType const UITextContentTypeUsername API_AVAILABLE(ios(11.0));
UIKIT_EXTERN UITextContentType const UITextContentTypePassword API_AVAILABLE(ios(11.0));
配置了如上代碼后,就會在點擊密碼輸入框的時候,顯示鑰匙圖標,點擊要是圖標后,就會顯示出已存儲的帳號密碼。并且,只要移除自己的登錄頁面的時候,系統就會自動彈出存儲或修改帳號密碼的彈框。
遇到問題
問題1:不顯示密碼填充的小鑰匙。
解決方法:開啟自動填充密碼:設置 -> 密碼與帳戶 -> 打開自動填充密碼
問題2:登錄成功后,沒有顯示存儲密碼,修改密碼系統彈框。
解決辦法:目前筆者在iOS13.0 的iPhone6s的設備上測試發現彈框顯示與否和 accountTextField.keyboardType = UIKeyboardTypeEmailAddress;
有關,大家如果遇到登錄成功后,沒有顯示存儲密碼彈框的問題,可以設置需要的keyboardText的類型試試看。
四、修改密碼
修改密碼的時候也可以使用,自動填充驗證的短信(點擊鍵盤上的Quick TypeBar,驗證碼自動填充如輸入框),及要使用的自動生成的密碼。
效果圖
鍵盤上方的QuickTypeBar顯示當前收到的短信
代碼
accountTextField.textContentType = UITextContentTypeUsername;
passwordTextField.textContentType = UITextContentTypeNewPassword;
smsCodeTextField.textContentType = UITextContentTypeOneTimeCode;
UITextContentTypeNewPassword
、UITextContentTypeOneTimeCode
適用于系統版本大于等于 iOS12的設備。
UIKIT_EXTERN UITextContentType const UITextContentTypeNewPassword API_AVAILABLE(ios(12.0));
UIKIT_EXTERN UITextContentType const UITextContentTypeOneTimeCode API_AVAILABLE(ios(12.0));
五、其他
添加帳號密碼
設置 -> 密碼與帳戶 -> 網站與App密碼 : 添加帳號密碼
根據網站、用戶名、密碼添加新的帳號密碼。
添加完畢后,在自動填充密碼的登錄頁面,可供選擇填充新增的用戶名密碼。
關于WebView相關的內容
關于WebView相關的內容,敬請查看 Enabling Password AutoFill on an HTML Input Element
和ASPasswordCredential的授權方法有關聯
以前做蘋果登錄Sign In With Apple(一) ,登錄成功的時候會調用如下授權成功的方法。
其中,我們的自動填充密碼,填充成功后,會調用到如下方法的ASPasswordCredential類型分支部分。
- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization API_AVAILABLE(ios(13.0)) {
if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]) {
// 存儲過帳號密碼的情況,授權登錄的時候會調用這里
}
}
六、Demo
示例代碼見:QiPasswordAutoFill
參考學習資料
- Password AutoFill
- Password Rules Validation Tool
- AutoPassword-iOS12-Demo
- iOS12 - Password AutoFill, Automatic Strong Password, and Security Code AutoFill
- iOS 12 Password Tools: Improving User Security and Experience
- iOS 11 ---- Password Auto Fill
推薦文章:
iOS 給UILabel添加點擊事件
用SwiftUI給視圖添加動畫
用SwiftUI寫一個簡單頁面
Swift 5.1 (7) - 閉包
iOS App啟動優化(三)—— 自己做一個工具監控App的啟動耗時
iOS App啟動優化(二)—— 使用“Time Profiler”工具監控App的啟動耗時
iOS App啟動優化(一)—— 了解App的啟動流程