UniWebView has a powerful event system to let you know what happens to the web view. You can know when the page loading successfully finished or failed with an error. The Unity SDK taps into those events to add functionality to the WebView.

Using these events, we want to return control back to the Unity game.

OnPageFinished

void Start () {

    //...

    webView.Show();

    webView.OnPageFinished += (view, statusCode, url) => {
        // Page load finished
    };
}

OnMessageReceived

void Start () {
    //...

    webView.OnMessageReceived += (view, message) => {
       
            // Intercept URL success or failure callbacks here
        }
    };
}

The action handler receives a message in which the URL is parsed. Here is where you check to see your success or failure callback URL parameters.

OnPageErrorReceived

Handle errors raised when the page during the loading process.

void Start () {

    //...

    webView.OnPageErrorReceived += (view, errorCode, errorMessage) => {
       
    };
}

OnShouldClose

This is an event sent to you when the web view is about to close. The users can use the Done button on the iOS toolbar or Back button on Android to close the web view. When the web view closed. the UniWebView component will be also destroyed automatically to keep things clean.

📘

Tip

On Android, the "Back" button will navigate your user back to the previous page. If there is no page to go back, it will send the OnShouldClose event and try to close the web view if true is returned

To do that, listen to the OnShouldClose event and reset webView at the end of Start method:

void Start () {

    //...

    webView.OnShouldClose += (view) => {
        webView = null;
        return true;
    };
}

If you choose to implement your custom webview solution to access the Carry1st Payment service, keep the webview open to intercept call-back URLs with the payment statuses. Closing the webview cancels the open payment flow. In our example, we intercept the URLs using Uniwebview's event listeners.