Skip to content

Can't See WebView on Android #1217

Description

@JoshTambo

I am using the WebView in Unity Editor and iOS just fine but every time I build to Android I'm getting a 401 or 404 error.

Below is the code that im using to open the Webview. I have hardware acceleration set to true and allow clear text to true with no luck.

I am using Unity 6000.3.14f1, Android Minimum SDK 26.

Please help

` IEnumerator OpenPaymentWebViewCoroutine(string mersoResponse, string itemName, string price, Action<string> OnSuccess, Action<string> OnFailed)
    {
        var json = JsonUtility.FromJson<MersoResponse>(mersoResponse);

        var checkoutUrl = $"http://merso-game-api.azurewebsites.net/api/payment/checkout" +
                          $"?clientSecret={UnityWebRequest.EscapeURL(json.clientSecret)}" +
                          $"&itemName={UnityWebRequest.EscapeURL(itemName)}" +
                          $"&amount={price}";

        Debug.Log(" Opening WebView: " + checkoutUrl);

        // Destroy existing WebView if any
        if (_webView != null)
        {
            Destroy(_webView);
            _webView = null;
        }

        var webViewGO = new GameObject("WebViewObject");
        _webView = webViewGO.AddComponent<WebViewObject>();

        _webView.Init(
            cb: (msg) =>
            {
                Debug.Log($"CallFromJS: {msg}");
                if (msg.Contains("PAYMENT_SUCCESS"))
                {
                    Debug.Log(" Payment successful!");
                    GrantItemToPlayer(OnSuccess, itemName, price);
                    CloseWebView();
                }
                else if (msg.Contains("PAYMENT_CANCELLED"))
                {
                    Debug.Log("Payment cancelled");
                    CloseWebView();
                    OnFailed.Invoke("Payment Cancelled");
                }
            },
            err: (msg) =>
            {
                Debug.LogError($"WebView error: {msg}");
                CloseWebView();
                OnFailed.Invoke("Payment Cancelled");
            },
            httpErr: (msg) =>
            {
                Debug.LogError($"WebView HTTP error: {msg}");
                CloseWebView();
                OnFailed.Invoke("Payment Cancelled");
            },
            started: (msg) =>
            {
                Debug.Log($"WebView started: {msg}");
                if (msg.Contains("PAYMENT_SUCCESS"))
                {
                    GrantItemToPlayer(OnSuccess, itemName, price);
                    CloseWebView();
                }
                else if (msg.Contains("PAYMENT_CANCELLED"))
                {
                    CloseWebView();
                    OnFailed.Invoke("Payment Cancelled");
                }
            },
            hooked: (msg) =>
            {
                Debug.Log($"WebView hooked: {msg}");
            },
            ld: (msg) =>
            {
                Debug.Log($"WebView loaded: {msg}");

                // Inject Unity bridge JS — required for Android and older iOS
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS
            var js = @"
                if (!(window.webkit && window.webkit.messageHandlers)) {
                    window.Unity = {
                        call: function(msg) {
                            window.location = 'unity:' + msg;
                        }
                    };
                }
            ";
#elif UNITY_ANDROID
            var js = @"
                window.Unity = {
                    call: function(msg) {
                        window.location = 'unity:' + msg;
                    }
                };
            ";
#else
                var js = "";
#endif

                _webView.EvaluateJS(js);
            },
            //androidForceDarkMode: 0,
            enableWKWebView: true,
            wkContentMode: 0,
            wkAllowsLinkPreview: false
        );

        // Wait for WebView to fully initialize before loading URL
        while (!_webView.IsInitialized())
        {
            yield return null;
        }

        _webView.SetMargins(0, 0, 0, 0);
        _webView.SetTextZoom(100); // Android only
        _webView.SetVisibility(true);
        _webView.LoadURL(checkoutUrl);
    }
``

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions