SwiftWebViewBridge icon indicating copy to clipboard operation
SwiftWebViewBridge copied to clipboard

Bridge stops receiving messages after call is cancelled/fails

Open steventnorris-40AU opened this issue 8 years ago • 1 comments

If you get a loading failure, calling didFailLoadWithError on your delegate, the bridge no longer receives or responds to any JS messages passed, as if the bridge connection is severed. In my case, I reload links into the same web view, so after a single failure, no subsequent loads handle JS messages.

steventnorris-40AU avatar Jun 20 '17 16:06 steventnorris-40AU

Further investigation:

If you instantiate the bridge and reconnect it to the webview on failure, you can get the bridge working again, but of course that's because I reconnect a new bridge.

In addition, if you declare a delegate after the bridge, the bridge does not work.

I use an "initializeBridge" method on init of the view. I then call that method again on my delegate's "didFailLoadWithError". This is a workaround, but does not seem to be intended functionality.

EX:

This works:

private func initializeBridge() {
        self.delegate = self

        bridge = SwiftWebViewBridge.bridge(self, defaultHandler: { _ , _ in print("DEFAULT") })
        bridge.registerHandlerForJS(handlerName: "shotPlaced") { jsonData, responseCallback in
            guard let jsonDict = jsonData as? [Double] else {
                print("SHOT PLACED")
                self.onShotPlaced?(.error)
                return
            }
            
            let pos: FloorPosition = (jsonDict[0], jsonDict[1])
            self.onShotPlaced?(.success(pos))
        }
        bridge.registerHandlerForJS(handlerName: "courtLoaded") { jsonData, responseCallback in
            print("JS MESSAGE")
            self.loadingIndicator.stopAnimating()
        }
    }

This does not:

private func initializeBridge() {
        bridge = SwiftWebViewBridge.bridge(self, defaultHandler: { _ , _ in print("DEFAULT") })
        bridge.registerHandlerForJS(handlerName: "shotPlaced") { jsonData, responseCallback in
            guard let jsonDict = jsonData as? [Double] else {
                print("SHOT PLACED")
                self.onShotPlaced?(.error)
                return
            }
            
            let pos: FloorPosition = (jsonDict[0], jsonDict[1])
            self.onShotPlaced?(.success(pos))
        }
        bridge.registerHandlerForJS(handlerName: "courtLoaded") { jsonData, responseCallback in
            print("JS MESSAGE")
            self.loadingIndicator.stopAnimating()
        }

        self.delegate = self
    }

steventnorris-40AU avatar Jun 20 '17 16:06 steventnorris-40AU