# Webview

The `userRedirectUrl` field is used to direct the user. This field is received in the success response of the process creation when making the CreateProcess request.&#x20;

Here you will find three ways to manage the user experience in web applications:

{% tabs %}
{% tab title="Android" %}

### **Step 1:** Using CustomTabs for Integration

1 - Add the necessary dependency for using CustomTabs in your app/build.gradle:

```
implementation("androidx.browser:browser:1.5.0")
```

### **Step 2:** Opening a CustomTab

```java
import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.browser.customtabs.CustomTabsClient
import androidx.browser.customtabs.CustomTabsIntent

class CustomTabActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)

       openCustomTab(<URL_CBU>)
    }

    fun openCustomTab(url: String) {
        val builder = CustomTabsIntent.Builder()
        val customTabsIntent = builder.build()
        customTabsIntent.launchUrl(this, Uri.parse(url))
    }
}
```

### **Step 3:** Modifying AndroidManifest

Add the necessary permissions and intents in the AndroidManifest.xml for the Activity that you want to receive the callback\_uri.\
It is necessary to include the attribute `android:launchMode="singleTop"` as well as the `<data>` tag providing the URI data.

```
xml
```

```java
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-permission android:name="android.permission.CAMERA"/> 
// ermissions for camera and geolocation are required

<activity
    android:name=".CustomTabActivity"
    android:exported="true"
    android:label="@string/app_name"
    android:theme="@style/Theme.Customtabs"
    android:launchMode="singleTop">

    <intent-filter android:label="Custom Tab">
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <!-- scheme e host são os dados fornecidos na criação de um processo no campo callback_uri
        callback_uri: "foobar://success?code=1234" -->
        <data android:scheme="foobar" android:host="success"/>
    </intent-filter>

</activity>
```

The following permissions are necessary for it to function correctly:

* Camera
* Geolocation

### **Step 4:** Getting Return Information

o retrieve redirect information with the provided data, you can use the following code in the `onNewIntent` method of your Activity:

```java
override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)

    val url = intent.data
    val scheme = url.scheme // "foobar"
    val host = url.host // "success"
    val code = url.getQueryParameter("code") // "1234"
}
```

{% endtab %}

{% tab title="iOS" %}

### 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:

```swift
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).

```swift
import SwiftUI
import AuthenticationServices
```

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

```swift
@State private var finished = false
```

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

```swift
let unicoController = UnicoAuthenticationController()
```

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

```swift
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()
    }
```

{% hint style="info" %}
**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.
{% endhint %}
{% endtab %}
{% endtabs %}

{% hint style="success" %}
It is also possible to use the link generated by Unico in hybrid frameworks. To do this, you can create a bridge between the framework used and the native one and follow as we suggest in the documents or use a library that makes these integration options available.
{% endhint %}

{% hint style="warning" %}
Integrating WebView into your application is the customer's sole responsibility, as this functionality is not offered as part of Unico's libraries or SDKs. Because of this, we do not offer technical support for questions or problems related to implementing WebView in your application. For configuration guidance, we recommend consulting the official documentation for the technology used in your project (e.g. React Native, Flutter, etc.).
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devcenter.unico.io/trust-en/integrations/integration-by-unico/controlling-the-experience/app/webview.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
