marcuswestin / WebViewJavascriptBridge

An iOS/OSX bridge for sending messages between Obj-C and JavaScript in UIWebViews/WebViews
http://marcuswest.in
MIT License
14.3k stars 2.98k forks source link

Issue getting iOS response after upgrading to 5.0.1 #184

Closed saraht129 closed 8 years ago

saraht129 commented 8 years ago

Hi there, I'm developing with Swift, WKWebView and iOS 9. I was using v4.1.5 and the bridging works fine. After upgrading to 5.0.1, the JS side failed to retrieve response from Swift.

I have updated my JS file according to your doc:

    function setupWebViewJavascriptBridge(callback) {
        if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
        if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
        window.WVJBCallbacks = [callback];
        var WVJBIframe = document.createElement('iframe');
        WVJBIframe.style.display = 'none';
        WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
        document.documentElement.appendChild(WVJBIframe);
        setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)
    }

I am also able to see the logs of send / receive activity on my Xcode console:

    2016-02-02 22:32:34.052 MyApp[42991:413976] WVJB RCVD: {
      "handlerName" : "myHandler"
    }

The app is able to stop at the breakpoint i place on my registerHandler line:

    self.bridge?.registerHandler("myHandler", handler: {[unowned self] (data: AnyObject!, callback: WVJBResponseCallback!) -> Void in
                callback(self.myHandler())
            })

But the JS just doesn't get the data returned on self.myHandler(), the console also doesn't print any response log.

What have I missed here? Thanks!

Some investigations:

  1. self.bridge is not nil
  2. self.bridge?.callHandler("jsHandler") works fine
marcuswestin commented 8 years ago

I'm on it. Give me just a few hours please - gotta make coffee first :)

-- while mobile

On Tue, Feb 2, 2016 at 9:37 AM, saraht129 notifications@github.com wrote:

Hi there, I'm developing with Swift, WKWebView and iOS 9. I was using v4.1.5 and the bridging works fine. After upgrading to 5.0.1, the JS side failed to retrieve response from Swift. I have update my JS file according to your doc: function setupWebViewJavascriptBridge(callback) { if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); } if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); } window.WVJBCallbacks = [callback]; var WVJBIframe = document.createElement('iframe'); WVJBIframe.style.display = 'none'; WVJBIframe.src = 'wvjbscheme://BRIDGE_LOADED'; document.documentElement.appendChild(WVJBIframe); setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0) } I am also able to see the logs of send / receive activity on my Xcode console: 2016-02-02 22:32:34.052 MyApp[42991:413976] WVJB RCVD: { "handlerName" : "myHandler" } The app is able to stop at the breakpoint i place on my registerHandler line: self.bridge?.registerHandler("myHandler", handler: {[unowned self](data: AnyObject!, callback: WVJBResponseCallback!) -> Void in callback(self.myHandler()) }) But the JS just doesn't get the data returned on self.myHandler(), the console also didn't print any response log. What have I missed here? Thanks! Some investigations:

  1. self.bridge is not nil

2. 1self.bridge?.callHandler("jsHandler")` works fine

Reply to this email directly or view it on GitHub: https://github.com/marcuswestin/WebViewJavascriptBridge/issues/184

saraht129 commented 8 years ago

Thanks for the quick response :) Enjoy your coffee

marcuswestin commented 8 years ago

@saraht129: I've added a test to ensure that the JS does indeed get values returned by the ObjC side of things. Here are the relevant lines:

ObjC responding to JS: https://github.com/marcuswestin/WebViewJavascriptBridge/blob/5e40fefddd790c31d162c6a71976e2942f275ea9/Tests/WebViewJavascriptBridgeTests/BridgeTests.m#L134

Javascript that checks the return value from ObjC: https://github.com/marcuswestin/WebViewJavascriptBridge/blob/5e40fefddd790c31d162c6a71976e2942f275ea9/Tests/WebViewJavascriptBridgeTests/echo.html#L24

I can help you debug the issue if you're able to reproduce it for me :)

Cheers, Marcus

marcuswestin commented 8 years ago

Also worth noting: the latest version is 5.0.2 as of a few days ago :)

saraht129 commented 8 years ago

Thanks for the response. I found the source of the issue, which is a reckless mistake:

The callHandler on my JS missed the data field: bridge.callHandler("myHandler", function (response){ }) (I misread your doc when updating my JS for 5.0.1)

Changing it back to bridge.callHandler("myHandler", null, function (response){ }) fixes the whole thing.

Thanks man :)

marcuswestin commented 8 years ago

Ah. The library could definitely be more helpful there! We should probably handle both cases properly. Adding to roadmap. Cheers!

-- while mobile

On Tue, Feb 2, 2016 at 11:17 AM, saraht129 notifications@github.com wrote:

Closed #184.

Reply to this email directly or view it on GitHub: https://github.com/marcuswestin/WebViewJavascriptBridge/issues/184#event-536076096