Implementing Password AutoFill for an iOS application
Learn how to activate Apple's Password AutoFill for SwiftUI and UIKit.
15 Aug 2022 · 3 min read
Apple's Password AutoFill feature allows users to login into the same account on different devices without having to remember their credentials.
For example, when signing up in a web app, users are able to generate a password and to save credentials in their iCloud keychain. When they run a native app to access the same account at some point, the app suggests the credentials stored for the website in the password QuickType bar. After the user authenticates, let's say by using Face ID, the system prefills the credentials - so there is no need to reenter them.
To activate Password Autofill in an iOS application, the following steps are required:
- Setting up the app's associated domains.
- Setting the correct AutoFill type on relevant text fields.
Let's look at those steps in more detail.
Setting up the app's associated domains for shared web credentials
Associated domains establish a secure association between a website and an iOS application. To support shared web credentials, the following steps are required:
- Creating and configuring an associated domain file for shared web credentials.
- Adding the associated domain file to the related website.
- Adding an associated domain entitlement to the iOS app.
Each of those steps is described in more detail in this quide on associated domain files.
Enable Password Autofill with textContentType
After setting up shared web credentials, all we need to do is to set the textContentType property on any relevant input views to activate the correct AutoFill suggestions.
The following UITextContentType values are available:
- .username for account names
- .password for passwords
- .newPassword for new passwords
- .oneTimeCode for one-time passwords, for example to autocomplete security codes from single-factor sms login flows
Let's see how to do that for SwiftUI and UIKit applications.
Enable Password Autofill for SwiftUI
TextField("Enter your email", text: $emailAddress).textContentType(.username).keyboardType(.emailAddress)TextField("Enter your password", text: $password).textContentType(.password)
Based on textContentType we provided, iOS automatically chooses a matching keyboard to show to the user.
To present a different keyboard, for example if the app uses emails as user names, we can additionally set the .keyboardType to a desired value.
Enable Password Autofill for UIKit
UIKit supports Password AutoFill on UITextField, UITextView and any custom view that adopts the UITextInput protocol. We can set a UITextContentType value by using the textContentType instance property:
userTextField.textContentType = .usernameuserTextField.keyboardType = .emailAddresspasswordTextField.textContentType = .password
Like to support my work?
Articles with related topics
Supporting universal links in a SwiftUI application
Learn how to implement universal links to be able to link to content in the iOS application and its connected website.
01 Aug 2022 · 3 min read
Quick guide on associated domain files for iOS development
Learn how to connect the iOS app with its related website to support features like universal links, shared web credentials or app clips.
25 Jul 2022 · 5 min read
Quick guide on Local Authentication for iOS
Learn how to use biometric authentication like Face ID or Touch ID.
24 Jan 2022 · 4 min read
Symmetric-key cryptography with CryptoKit for iOS
Using CryptoKit to increase security in iOS applications.
17 Jan 2022 · 4 min read
Cryptographic hash functions in CryptoKit for iOS
Use cases of cryptographic hash functions and how to implement them in iOS.
10 Jan 2022 · 3 min read
Latest articles and tips
Memory management for async/await and tasks in Swift
Learn about weak self references when working with the async/await API.
03 Oct 2022 · 2 min read
How to manage lifecycle events in SwiftUI iOS applications
Discover SwiftUI's lifecycle events onAppear(), onDisappear() and task().
26 Sep 2022 · 3 min read
iOS developer guide on the main aspects of Core Data
Understand the main concepts of the Core Data framework.
19 Sep 2022 · 5 min read
Memory management in Combine
Learn how to avoid retain cycles when using the Combine framework in Swift.
12 Sep 2022 · 4 min read
How to build a configurable widget with WidgetKit and SwiftUI
Learn how to allow users to customize their widgets.
05 Sep 2022 · 4 min read