ASWebAuthenticationSession

In this section, you will find how to implement the webview in iOS using ASWebAuthenticationSession.


For iOS usage, using ASWebAuthenticationSession is one of the recommended approaches.

After creating the transaction and obtaining the transaction link, the following implementation is recommended:

  • In your regular flow (which includes IDPay), you will open the ASWebAuthenticationSession with the link generated via API.

  • You can customize this opening in the way that works best for your app.

  • You will monitor if the URL has changed (to the redirectUrl) and then close the page.

To make the flow work, you need to follow the following steps:

Step 1: Create the Payment Authentication Controller


The first step is to create the payment authentication controller. To do this, create a class called IDPayAuthenticationController (or any name you prefer).

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

Declare the class as NSObject and implement the ASWebAuthenticationPresentationContextProviding protocol.

The result should be:

import AuthenticationServices

class IDPayAuthenticationController: 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


Open the file where you will perform the authentication and add the necessary imports (in our example, we are doing this in ContentView.swift).

import SwiftUI
import AuthenticationServices

To control the authentication state, we will create a @State property.

@State private var isAuthenticated = false

Create an instance of the IDPayAuthenticationController class outside the body of the ContentView structure.

let idPayController = IDPayAuthenticationController()

To validate the payment, create a function called authenticatePayment.

func authenticatePayment() {
    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 a autenticação: \(error.localizedDescription)")
            }
            return
        }

        // Processa o URL de callback para verificar se a autenticação foi bem-sucedida
        session?.cancel()
        isAuthenticated = true
    }

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

Remember to change the URL_AUTHENTICATION to the authentication URL received in your transaction and also the callbackURLScheme BUNDLE to the redirect provided when creating your transaction (we recommend using the Bundle Identifier of your app).

It is important to set prefersEphemeralWebBrowserSession to true to ensure a unique authentication per transaction.

Example of how it should look in the app:

Some permissions are required for it to work properly, such as:

  • Camera;

  • Geolocation.

For more information, we recommend reading the following articles and documentation:

To access the official documentation, click here.


Still need help?

Didn't find something or still need help? If you're already a client or partner, you can reach out through our Help Center.

Last updated

Copyright © 2024 unico. All rights reserved.