ASWebAuthenticationSession


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

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

  • In your common flow (which IDPay is inserted) you will open the ASWebAuthenticationSession with the link generated through the API;

  • Customize this opening in any way that is ideal for your application;

  • Monitor if the URL has changed (to redirectUrl) and then close the WKWebView;

To make the flow work, follow these steps:

Step 1: Create the payment authentication controller


OCreate a class called IDPayAuthenticationController (or whatever you prefer to call it).

Then, import the AuthenticationServices ramework 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 (the example is done in ContentView.swift).

import SwiftUI
import AuthenticationServices

To control the authentication state, 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. Also change the callbackURLScheme BUNDLE to the redirect informed at the creation of your transaction (use the Bundle Identifier of your application).

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

Follows an example of how it should look in the APP:

The following permissions are required to function correctly:

  • Camera;

  • Geolocation.

To know more about the WKWebView, the following articles and documentation are recommended:

  • Access the official documentation at WKWebView.


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.