iOS

The userRedirectUrl field is used to redirect the user. This field is received in the successful process creation response when making the Process Creation request. Here you will find the best way to manage the user experience, using ASWebAuthenticationSession, in your iOS application:

Step 1: Create the authentication controller

1 - The first step is to create the authentication controller, so create a class called UnicoAuthenticationController (or any name you prefer).

2 - Next, import the AuthenticationServices framework at the top of the class.

3 - Declare the class as NSObject and implement the ASWebAuthenticationPresentationContextProviding protocol.

  • The result should be:

import AuthenticationServices

class UnicoAuthenticationController: NSObject, ASWebAuthenticationPresentationContextProviding {
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
           if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
               if let mainWindow = windowScene.windows.first {
                   return mainWindow
               }
           }
           return ASPresentationAnchor()
       }
}

Step 2: Implement authentication

1 - Open the file where you perform the authentication and add the necessary imports (for example, ContentView.swift is used).

import SwiftUI
import AuthenticationServices

2 - To control the flow state, create the @State property.

@State private var finished = false

3 - Create an instance of the UnicoAuthenticationController class outside the body of the ContentView struct.

let unicoController = UnicoAuthenticationController()

4 - For process validation, create a function called redirectUser.

func redirectUser() {
        guard let url = URL(string: "URL_AUTHENTICATION") else { return }

        var session: ASWebAuthenticationSession?
        session = ASWebAuthenticationSession(url: url, callbackURLScheme: "BUNDLE") { callbackURL, error in
            guard callbackURL != nil else {
                if let error = error {
                    return print("Erro durante o processo: \(error.localizedDescription)")
                }
                return
            }

            // Process the callback URL to check whether the process was completed.
            session?.cancel()
            finished = true
        }

        session?.presentationContextProvider = unicoController
        session?.prefersEphemeralWebBrowserSession = true
        session?.start()
    }

Environments:

Remember to update the URL_AUTHENTICATION to the authentication URL provided for your process, and also update the callbackURLScheme BUNDLE to the redirect value specified during the process creation (it is recommended to use your app’s Bundle Identifier).

Single authentication:

It’s important to set prefersEphemeralWebBrowserSession to true to ensure a single authentication per process.

Last updated

Was this helpful?