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

Invoke unity function from Javascript or listen/trigger from redirect http response #220

Closed
paatz04 opened this issue Jul 17, 2017 · 9 comments

Comments

@paatz04
Copy link

paatz04 commented Jul 17, 2017

Hey there! Awesome work.

I was wondering if there is a way to listen to javascript function and invoke a unity function.
I couldn't find a documentation. Any hints ?

I would like to build a user login form and a short quizz and use that in my webview. I need to somehow listen to the submit event and close the webform. Or can i register to redirects ? For example close the webview when a certain response is returned.

Do I have to work with the init function I suppose ? I assume I can use cb - > call from JS. Do you have a JS sample that would trigger cb?

Thanks!

@KojiNakamaru
Copy link
Member

The sample app shows how to interract between Unity and HTML. If you want to call the Unity-side, you can utilize Unity.call(), for example:

<p><a href="javascript:void(0)" onclick="Unity.call('anchor');">[Send Message!]</a></p>

If you want to call the JavaScript-side, you can utilize EvaluateJS(), for example:

<p><a href="javascript:void(0)" onclick="Unity.call('anchor');">[Send Message!]</a></p>

@paatz04
Copy link
Author

paatz04 commented Jul 17, 2017

Hey! Thanks for the quick and prompt reply!
So : <p><a href="javascript:void(0)" onclick="Unity.call('anchor');">[Send Message!]</a></p>
Calls the cb action added to the webViewObject.init() with the msg="anchor" ?

@KojiNakamaru
Copy link
Member

Yes, it behaves as you noted. So you can communicate between Unity and HTML (JavaScript) in both directions.

@paatz04
Copy link
Author

paatz04 commented Aug 10, 2017

Thanks a lot! Easy and simple. I'm about to use it in production. Are you aware of any limitations on iOS / android ? Does it support Android SDK >=15 and iOS >=8? Cheers
It works fine on my devices 👍

@KojiNakamaru
Copy link
Member

Yes, it should run well on android with API version 15 or later and iOS 8 or later.

@paatz04
Copy link
Author

paatz04 commented Aug 31, 2017

what is a production ready pre-processor flag for android and iOS. The sample app uses:

#if !UNITY_ANDROID // NOTE: depending on the situation, you might prefer // the 'iframe' approach. // cf. https://github.com/gree/unity-webview/issues/189 #if true webViewObject.EvaluateJS(@" window.Unity = { call: function(msg) { window.location = 'unity:' + msg; } } "); #else webViewObject.EvaluateJS(@" 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 #endif

But that doesn't work on android at all.
Does going with

webViewObject.EvaluateJS(@" window.Unity = { call: function(msg) { window.location = 'unity:' + msg; } } ");

Work in any case for android >15 and ios>8. Or what do you suggest?

When calling LoadURL(), is there a way to clear the webview (show white while loading) instead of showing the previous website until it finishes loading ?

I saw you were referencing this issue: #189
Thanks!

@KojiNakamaru
Copy link
Member

The sample app doesn't define Unity.call():

#if !UNITY_ANDROID
// NOTE: depending on the situation, you might prefer
// the 'iframe' approach.
// cf. https://github.com/gree/unity-webview/issues/189
#if true
webViewObject.EvaluateJS(@"
window.Unity = {
call: function(msg) {
window.location = 'unity:' + msg;
}
}
");
#else
webViewObject.EvaluateJS(@"
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
#endif

because Unity.call() is defined in WebViewPlugin.jar:

@JavascriptInterface
public void call(final String message) {
call("CallFromJS", message);
}

webView.addJavascriptInterface(mWebViewPlugin , "Unity");

When calling LoadURL(), is there a way to clear the webview (show white while loading) instead of showing the previous website until it finishes loading ?

You could utilize EvaluateJS() and/or LoadHTML() to show some loading animation while redirecting to the next page.

@paatz04
Copy link
Author

paatz04 commented Sep 1, 2017

Thank you!!! That means it should work without the injection for android, so I can use the compiler flag, and therefore only inject it through EvaluateJs for iOS or does it do a similar thing for iOS?
I noticed injecting it for android as well works anyway though

At the moment it works fine with your sample.html but the same call on my page isn't fired. Any tipps on how to debug ?

@KojiNakamaru
Copy link
Member

Thank you!!! That means it should work without the injection for android, so I can use the compiler flag, and therefore only inject it through EvaluateJs for iOS or does it do a similar thing for iOS?

The injection is required only for iOS. If you only utilize WKWebView (by specifying enablWKWebView: true and limiting iOS versions to 8 or later), you can utilize more efficient/reliable window.webkit.messageHandlers.unityControl.postMessage() (introdueced by @dragonx in #161) instead of Unity.call() and don't need the injection.

I noticed injecting it for android as well works anyway though

Yes, it also works because unity: prefixed URLs can also work for Android. However, natively defined Unity.call() should be more efficient.

@paatz04 paatz04 closed this as completed Sep 7, 2017
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