ShawnFoo / SwiftWebViewBridge

Swift version of WebViewJavascriptBridge with more simplified and friendly methods to handle messages between Swift and JS in UIWebViews
MIT License
137 stars 34 forks source link

Fixed key lookup when looking for swift callback. #12

Closed carlonluca closed 7 years ago

carlonluca commented 7 years ago

Callback was not invoked. It is my understanding the structure is placed inside the "data" member.

ShawnFoo commented 7 years ago

Hi, @carlonluca Would you mind telling your test case that caused callback not invoke? I tested all the cases in demo project and it worked just fine.

As for the key, "data", is for storing json data from the side either Swift or Javascript when calling the other 's handler. The callbacks are placed in the each side of Swift and Javascript which are not passed as json object from both sides.

For example, I call the one handler registered in Javascript with the "handlerName""data" and "callbackId", levaing the callback closure in Swift. And after SwiftWebViewBridge.js executed the function matched the "handlerName" with the "data", it will store the response json object in "responseData" if the value of "callbackId" is not null, also the "callbackId" will be the "responseId" when the next time Swift ask Javascript for messages. And use that "responseId"to get left callback back if not nil.

So if I check if the "responseId" is nil first, because it must be the case that Javascript calls the Swift's handler without "responseId". That 's why we can't count on the "data" value to distinguish the case is Javascript calls Swift's handler or is Swift handle the response json object from the Javascript.

Also you might want see in the Unminified javascript file in: ./UnminifiedJavascript/ SwiftWebViewBridge.js file.

And thanks.

carlonluca commented 7 years ago

It is probably me not properly understanding the interface then. Referring to the demo app, I'd expect https://github.com/ShawnFoo/SwiftWebViewBridge/blob/master/SwiftWebViewBridgeDemo/SwiftWebViewBridgeDemo/ViewController.swift#L117 and https://github.com/ShawnFoo/SwiftWebViewBridge/blob/master/SwiftWebViewBridgeDemo/SwiftWebViewBridgeDemo/ViewController.swift#L130 to be called once SendDataToJSWithCallBack and CallJSHandlerWithCallBack respectively are pressed. What I see here is that only the default handler is called instead.

carlonluca commented 7 years ago

As I see the structure is similar when passing the object this patch takes that into consideration. Not sure this is how the interface was intended but I needed those callbacks.

ShawnFoo commented 7 years ago

@carlonluca You're right, there is a bug that all callbacks run into the default handler's closure. I'll merge your request first. Later commit the refactoring version and update the cocoapods. Thank you! 👍 Can't imagine how long this bug has been existed : [