* * * * tag: “iOS” “Password AutoFill” author: WYW Review: QiShare team


Introduction: Password AutoFill

After a successful login, the account and password are saved in the KeyChain of the iCloud. When you log in to the iCloud next time, you can fill in the stored account and password and automatically fill in the SMS verification code. It simplifies the login and startup process using apps and web pages. Native UITextField and UITextView can be configured. The password saved after login in Safari browser can also be automatically filled in the App.

I. Preparation of Password AutoFill

Project configuration

Xcode configuration

TARGETS -> Signing & Capabilities -> Associated Domains

Server Configuration

Add the apple-app-site-association file to the directory on the server. The file contains the following JSON content:

{ 
    "webcredentials": { 
    	"apps": ["teamID.bundleID"]}}Copy the code
Terms of inspection

Simple verification: Access https:// your domain name /. Well-known /apple-app-site-association to access the corresponding file.

2. Register prompt and fill in strong password

UITextField or UITextView Sets the text type of the user name and password.

If we want users to have the option of automatically generating a password when they register. You can use passwordTextField. TextContentType = UITextContentTypeNewPassword; Specifies an auto-fill password.

rendering

code

accountTextField.textContentType = UITextContentTypeUsername;
accountTextField.keyboardType = UIKeyboardTypeEmailAddress;

passwordTextField.textContentType = UITextContentTypeNewPassword;
passwordTextField.secureTextEntry = YES;
Copy the code

UITextContentTypeUsername and UITextContentTypeNewPassword apply to the system version is greater than or equal to iOS11 equipment.

UIKIT_EXTERN UITextContentType const UITextContentTypeUsername                  API_AVAILABLE(ios(11.0));
UIKIT_EXTERN UITextContentType const UITextContentTypePassword                  API_AVAILABLE(ios(11.0));
Copy the code

We can have our own rules for password padding rules, which can be configured using UITextInputPasswordRules.

// Configure password rules
passwordTextField.passwordRules = [UITextInputPasswordRules passwordRulesWithDescriptor:@"required: lower; required: upper; required: digit; required: [-]; required: [&]; required: [+]; minlength: 20;"];
Copy the code

The password must contain lowercase letters, uppercase letters, digits, hyphens (-), ampersand (&), and +. The minimum length of the password must be 20 characters. See password Verification Tool for more information.

UITextInputPasswordRules applies to devices whose operating system version is later than or equal to iOS12.

UIKIT_EXTERN API_AVAILABLE(ios(12.0)) @interface UITextInputPasswordRules : NSObject <NSSecureCoding.NSCopying>
Copy the code

Have a problem

Fault 1: The iCloud Keychain is disabled

[AutoFill] Cannot show Automatic Strong Passwords for app bundleID: bundleID content Due to error: iCloud Keychain is disabled

This error occurs because the iCloud Keychain is not enabled.

The solution is: Set iCloud keychain Settings -> click the name -> click iCloud -> Click keychain -> Open.

3. Login prompt and fill in the password of the stored account

This configuration is required when we want to save the account and password. TextContentType corresponding to the account and password input fields.

rendering

The QuickTypeBar above the keyboard displays the small keys

The saved account passwords are displayed

The system automatically stores the password after a successful login

Change the password of a saved login account

code

accountTextField.textContentType = UITextContentTypeUsername;
accountTextField.keyboardType = UIKeyboardTypeEmailAddress;

passwordTextField.textContentType = UITextContentTypePassword;
passwordTextField.secureTextEntry = YES;
Copy the code

UITextContentTypeUsername, UITextContentTypePassword is suitable for system version is greater than or equal to iOS11 equipment.

UIKIT_EXTERN UITextContentType const UITextContentTypeUsername                  API_AVAILABLE(ios(11.0));
UIKIT_EXTERN UITextContentType const UITextContentTypePassword                  API_AVAILABLE(ios(11.0));
Copy the code

This will display the key icon when you click on the password input box, and the stored account password will be displayed when you click on the if icon. When you remove your login page, the system automatically displays a dialog box asking you to save or change your password.

Have a problem

Problem 1: Small keys filled with password are not displayed.

Solution: Enable automatic password filling: Settings -> Passwords and Accounts -> Enable Automatic password filling

Fault 2: After a successful login, the storage password is not displayed. Change the password.

Solution: currently, the author found in iOS13.0 iPhone6s device test play box shows or not and accountTextField keyboardType = UIKeyboardTypeEmailAddress; You can set the type of keyboardText you need to try if you do not display the stored password box after login successfully.

4. Change the password

It can also be used when changing passwords, auto-fill verification SMS (click the Quick TypeBar on the keyboard, the verification code is automatically filled as input field), and auto-generate password to use.

rendering

The QuickTypeBar at the top of the keyboard displays the current received SMS messages

code

accountTextField.textContentType = UITextContentTypeUsername;
passwordTextField.textContentType = UITextContentTypeNewPassword;
smsCodeTextField.textContentType = UITextContentTypeOneTimeCode;
Copy the code

UITextContentTypeNewPassword, UITextContentTypeOneTimeCode is suitable for system version is greater than or equal to iOS12 equipment.

UIKIT_EXTERN UITextContentType const UITextContentTypeNewPassword               API_AVAILABLE(ios(12.0));
UIKIT_EXTERN UITextContentType const UITextContentTypeOneTimeCode               API_AVAILABLE(ios(12.0));
Copy the code

Five, the other

Adding an Account Password

Settings -> Password and Account -> Website and App Password: Add account password

Add a new account password based on the website, user name, and password.

After the new user name and password are added, you can add the new user name and password on the login page for automatic password filling.

Something about WebView

Enabling Password AutoFill on an HTML Input Element for WebView

Associated with the authorization method of ASPasswordCredential

Before doing Apple login Sign In With Apple (a), the login success will call the following authorization success method.

Where our auto-fill password is successfully populated, the ASPasswordCredential branch section of the following method is called.

- (void)authorizationController:(ASAuthorizationController *)controller didCompleteWithAuthorization:(ASAuthorization *)authorization  API_AVAILABLE(ios(13.0)) {
    
    if ([authorization.credential isKindOfClass:[ASPasswordCredential class]]) {
        
        // If the account password has been stored, it will be called when authorized to log in}}Copy the code

Sixth, the Demo

See QiPasswordAutoFill for an example code

Reference study materials

  • 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

Recommended articles:

IOS add click events to UILabel with SwiftUI Add animation to view with SwiftUI Write a simple page Swift 5.1 (7) – closure iOS App startup optimization (three) — make a tool to monitor the App startup time iOS App startup optimization (2) — use “Time Profiler” tool to monitor App startup Time iOS App startup optimization (1) — understand the App startup process qidance Weekly