Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regarding the issue with CallFromJS in WKWebView! Seeking help. #995

Open
jagerrrrr opened this issue Oct 19, 2023 · 4 comments
Open

Regarding the issue with CallFromJS in WKWebView! Seeking help. #995

jagerrrrr opened this issue Oct 19, 2023 · 4 comments

Comments

@jagerrrrr
Copy link

jagerrrrr commented Oct 19, 2023

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
        );
@KojiNakamaru
Copy link
Member

I've put a modified sample which can be directly opened and built with 2020.3.15f2:

https://github.com/gree/unity-webview/tree/example-for-995/sample

You can check how it was changed from the latest master branch:

master...example-for-995

The js definition in the ld: callback is removed as below:

d5c641b

At the end of the callback, the code still performs the following and this invokes CallFromJS().

                webViewObject.EvaluateJS("Unity.call('ua=' + navigator.userAgent)");

@jagerrrrr
Copy link
Author

抱歉,我的意思是我拿到不网站中的js发送事件,关于这点您可以知道一下吗,任何形式的细节都可能帮助我,比如网页端需要如何写Unity.call(),为此需要导入什么库才能正确触发我们的这webview中的cb:事件

@KojiNakamaru
Copy link
Member

Please just try https://github.com/gree/unity-webview/tree/example-for-995/sample at first. As noted before, this sample app invokes Unity.call('ua=' + navigator.userAgent) and its message is received by the cb: callback.

@jagerrrrr
Copy link
Author

thank you!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants