UnitySendMessage duplicate?

Hi,
I’m working on a plugin for iOS for our company.
We try to load ads with Webview and we need to do callback to Unity.

We are using this function to do callbacks to Unity from JS code:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *url = [[request URL] absoluteString];
if ([url hasPrefix:@"unity:"]) {
        NSLog(@"Sending Unity event from JS");
UnitySendMessage([gameObjectNameUTF8String],
"CallFromJS", [[url substringFromIndex:6] UTF8String]);
returnNO;
} else {
returnYES;
}
}

When the Webview call this, here is what it logs in xCode:

Then if we destroy the Webview, and recreate another after, this log appears one time, but Unity logs 2 callback instead of one. If we destroy again the WebView, the next instance will call 3 times the callback.

Any idea of why ?

PS: We are really destroying the object before creating the new one, we have logs in dealloc of our object and the logs are called. Everything seams fine on the Obj-C side, I think the problem is on Unity side but I can’t find what.

Thank you for your help.

one potential pitfall is that UnitySendMessage will actually just queue the call. So you need to run player loop to actually make the call. I am not sure what you mean by “logging callback” etc, so that might be not the answer you are looking for - but it is good to know these details :wink:

Hi,
What I want to do is send a callback to a server when the page inside the webview is loaded.
To do it, I evaluate a javascript code that do something like Unity.call inside the JS of the page.
This function Unity.call basically create an iframe with a src prefixed with “unity:”
This way I can track in Obj-C when JS calls this function with shouldStartLoadWithRequest function.

Then if this function detects the prefix unity: , it calls a Unity function with UnitySendMessage.

With NSLog I can confirm that shouldStartLoadWithRequest is called 1 time.
But on the Unity side, my function is called as many time as the webview have been created before.
So if I open 2 webviews, that I destroy and after I create a new one, the function in Unity will be called 3 times but the function shouldStartLoadWithRequest in Obj-C is called one time.

Bump

Could you please post bit longer log of your application, where we can see both ObjC and Unity logs in single flow.

I modified my app to do it differently but I will give you a typical scheme of the bug:

  • Create a UIWebview and attach it to the Unity view.

  • Load a page

  • Evaluate JS in this page and create a function Unity.call of this type:

window.Unity = {
    call:function(msg) {
    if(msg == 'closeAd'){
        var iframe = document.createElement('IFRAME');
        iframe.setAttribute('src', 'unity:' + msg);
        document.documentElement.appendChild(iframe);
        iframe.parentNode.removeChild(iframe);
        iframe = null;
     }
};
  • Add this function to your Obj-C class:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *url = [[request URL] absoluteString];
    if ([url hasPrefix:@"unity:"]) {
        NSLog(@"Sending Unity event from JS");
        UnitySendMessage([gameObjectName UTF8String],
            "CallFromJS", [[url substringFromIndex:6] UTF8String]);
        return NO;
    } else {
        return YES;
    }
}
  • This function will intercept all the url loading, so when you call Unity.call in JS, this will call a function in Unity called “CallFromJS”
  • You can do a debug in the CallFromJS function
  • It will display at this time
  • Then I destroy my webview by using the release function and it displays my debug in the dealloc function
- (void)dealloc

{

    NSLog(@"Dealloc Webview");

[webViewremoveFromSuperview];

[webViewrelease];

[gameObjectNamerelease];

[superdealloc];

}
  • Then I create a new webview and I do exactly the same.
  • When i will call the Unity.call function, this will debug this:

So maybe I do something wrong but I search for days the problem and couldn’t find it. Moreover when I debug the webview in Safari, there is only one declaration of the Unity.call function so I really think this is on the Unity side.

Thank you.

Random idea. Maybe you are spawning/cloning gameobject, which is receiving the message, each time new webview is created?

But when I do the exact same thing on Android, the function is called once…
(Except that I don’t have to do an iframe to call a function from JS to Java)