iOS
In this section, you will find how to implement the webview on iOS for using the Unico IDPay product.
The link field is used to direct the user. This field is received in the transaction creation success response.
Here you'll find the best way to manage the user experience in your iOS application:
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 AuthenticationServicesTo control the authentication state, we will create a @State property.
@State private var isAuthenticated = falseCreate 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).
Example of how it should look in the app:

For more information, we recommend reading the following articles and documentation:
To access the official documentation, click here.
Last updated
Was this helpful?