gree / gree/unity-webview

Regarding the issue with CallFromJS in WKWebView! Seeking help.

Open
#995 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Objective-C++
Stars
2.7k
Forks
734
Avg merge
11h 39m
Merged PRs (30d)
1

Description

Hello, author.
I've integrated WKWebView into Unity, and in my web page, I've set up Unity.call(msg). However, my iOS application is not receiving the CallFromJS event. After adding the "ld:" JavaScript definition, I was able to trigger the CallFromJS event, but the msg I received isn't what I expected, it contains some hardware parameters. I noticed a comment stating that WKWebView doesn't need to include the "ld:" definition. So, what should I do? I've been struggling with this for a whole week.
I'm sorry,I'm very frustrated.

Log regarding hardware parameter information :

CallFromJS[ua=Mozilla/5.0 (iPhone; CPU iPhone OS 16_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148]

Code related to WebView initialization:

contentObject = new GameObject("ContentObject").AddComponent<WebViewObject>();
        contentObject.Init(
            cb: (msg) =>
            {
                Debug.Log(string.Format("CallFromJS[{0}]", msg));
                OnMsg?.Invoke(msg);
            },
            err: (msg) =>
            {
                Debug.Log(string.Format("CallOnError[{0}]", msg));
            },
            httpErr: (msg) =>
            {
                Debug.Log(string.Format("CallOnHttpError[{0}]", msg));
            },
            started: (msg) =>
            {
                Debug.Log(string.Format("CallOnStarted[{0}]", msg));
            },
            hooked: (msg) =>
            {
                Debug.Log(string.Format("CallOnHooked[{0}]", msg));
            },
            cookies: (msg) =>
            {
                Debug.Log(string.Format("CallOnCookies[{0}]", msg));
            },
            ld: (msg) =>
            {
                Debug.Log(string.Format("CallOnLoaded[{0}]", msg));
#if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_IOS
                // NOTE: the following js definition is required only for UIWebView; if
                // enabledWKWebView is true and runtime has WKWebView, Unity.call is defined
                // directly by the native plugin.
#if true
                var js = @"
                    if (!(window.webkit && window.webkit.messageHandlers)) {
                        window.Unity = {
                            call: function(msg) {
                                window.location = 'unity:' + msg;
                            }
                        };
                    }
                ";
#else
                // NOTE: depending on the situation, you might prefer this 'iframe' approach.
                // cf. https://github.com/gree/unity-webview/issues/189
                var js = @"
                    if (!(window.webkit && window.webkit.messageHandlers)) {
                        window.Unity = {
                            call: function(msg) {
                                var iframe = document.createElement('IFRAME');
                                iframe.setAttribute('src', 'unity:' + msg);
                                document.documentElement.appendChild(iframe);
                                iframe.parentNode.removeChild(iframe);
                                iframe = null;
                            }
                        };
                    }
                ";
#endif
#elif UNITY_WEBPLAYER || UNITY_WEBGL
                var js = @"
                    window.Unity = {
                        call:function(msg) {
                            parent.unityWebView.sendMessage('contentObject', msg);
                        }
                    };
                ";
#else
                var js = "";
#endif
                contentObject.EvaluateJS(js + @"Unity.call('ua=' + navigator.userAgent)");
            },
            enableWKWebView: true
        );

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The relevant entry point shown is WebViewObject.Init with enableWKWebView: true; the ld callback calls EvaluateJS and defines Unity.call. Start by tracing that WKWebView path against the window.location fallback, then reproduce the logged ua=... CallFromJS value and verify what message the callback receives.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, ios, unity
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.