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
Developer guide on App Clips for iOS
Learn how support App Clips to allow users to perform a specific task without installing the full iOS application.
24 Apr 2023 · 4 min read
Developer guide on passkeys for iOS
Learn how passkeys work and how to implement them in an iOS application.
27 Feb 2023 · 5 min read
Developer guide on keychain for iOS
Learn how to securely store passwords with Apple's Keychain API.
23 Jan 2023 · 4 min read
Latest articles and tips
Introduction to Kotlin for Swift developers - protocols, extensions & generics
Learn how protocols, extensions and generics in Kotlin work from a Swift developer's point of view.
02 Dec 2023 · 8 min read
How to store images in SwiftData
Learn how to efficiently store larger amounts of data in SwiftData.
27 Nov 2023 · 1 min read
How to migrate to a new schema with SwiftData in iOS
Learn how to create a migration plan in SwiftData.
20 Nov 2023 · 4 min read
How to implemet a free trial period for StoreKit 2 subscriptions in iOS
Learn how to implement StoreKit 2 introductory offers.
13 Nov 2023 · 2 min read
How to migrate from Localizable.strings to String Catalogs
Learn how to migrate your iOS project to .xcstrings for localization.
06 Nov 2023 · 1 min read