Page updated Nov 8, 2023

Enable sign-in with web UI

Amplify iOS v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Swift to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for iOS, you can access the documentation here.

Prerequisites

  • An iOS application targeting at least iOS 11.0 with Amplify libraries integrated

Configure Auth Category

This library's Cognito plugin currently supports the Authorization Code Grant OAuth Flow.

In terminal, navigate to your project, run amplify add auth, and choose the following options:

1? Do you want to use the default authentication and security configuration?
2 `Default configuration with Social Provider (Federation)`
3? How do you want users to be able to sign in?
4 `Username`
5? Do you want to configure advanced settings?
6 `No, I am done.`
7? What domain name prefix you want us to create for you?
8 `(default)`
9? Enter your redirect signin URI:
10 `myapp://`
11? Do you want to add another redirect signin URI
12 `No`
13? Enter your redirect signout URI:
14 `myapp://`
15? Do you want to add another redirect signout URI
16 `No`
17? Select the social providers you want to configure for your user pool:
18 `<hit enter>`

Once finished, run amplify push to publish your changes.

Update Info.plist

Signin with web UI require the Amplify plugin to show up the signin UI inside a webview. After the signin process is complete it will redirect back to your app. You have to enable this in your app's Info.plist. Right click Info.plist and then choose Open As > Source Code. Add the following entry in the URL scheme:

1<plist version="1.0">
2
3 <dict>
4 <!-- YOUR OTHER PLIST ENTRIES HERE -->
5
6 <!-- ADD AN ENTRY TO CFBundleURLTypes for Cognito Auth -->
7 <!-- IF YOU DO NOT HAVE CFBundleURLTypes, YOU CAN COPY THE WHOLE BLOCK BELOW -->
8 <key>CFBundleURLTypes</key>
9 <array>
10 <dict>
11 <key>CFBundleURLSchemes</key>
12 <array>
13 <string>myapp</string>
14 </array>
15 </dict>
16 </array>
17
18 <!-- ... -->
19 </dict>

When creating a new SwiftUI app using Xcode 13 no longer require configuration files such as the Info.plist. If you are missing this file, click on the project target, under Info, Url Types, and click '+' to add a new URL Type. Add myapp to the URL Schemes. You should see the Info.plist file now with the entry for CFBundleURLSchemes.

Launch Web UI Sign In

Sweet! You're now ready to launch sign in with web UI. The signInWithWebUI api require a presentationAnchor and for an iOS app it will be the main UIWindow of the app. The example code below assume that you are in a UIViewController where you can fetch the UIWindow instance by self.view.window.

1func signInWithWebUI() {
2 Amplify.Auth.signInWithWebUI(presentationAnchor: self.view.window!) { result in
3 switch result {
4 case .success:
5 print("Sign in succeeded")
6 case .failure(let error):
7 print("Sign in failed \(error)")
8 }
9 }
10}
1func signInWithWebUI() -> AnyCancellable {
2 Amplify.Auth.signInWithWebUI(presentationAnchor: self.view.window!)
3 .resultPublisher
4 .sink {
5 if case let .failure(authError) = $0 {
6 print("Sign in failed \(authError)")
7 }
8 }
9 receiveValue: { _ in
10 print("Sign in succeeded")
11 }
12}

Prefer private session during signIn

Starting Amplify 1.6.0, Amplify.Auth.signInWithWebUI automatically uses ASWebAuthenticationSession internally for iOS 13.0+. For older iOS versions, it will fall back to SFAuthenticationSession. This release also introduces a new preferPrivateSession flag to AWSAuthWebUISignInOptions during the sign in flow. If preferPrivateSession is set to true during sign in, the user will not see a web view displayed when they sign out. preferPrivateSession will set ASWebAuthenticationSession.prefersEphemeralWebBrowserSession internally and the authentication session will be private if the user's preferred browser supports it.

1Amplify.Auth.signInWithWebUI(presentationAnchor: self.view.window!,
2 options: .preferPrivateSession()) { ... }