From 0b7c995915232a40bc0b0fdb13232afe33d6d8b7 Mon Sep 17 00:00:00 2001 From: Marcus Westin Date: Tue, 27 Dec 2016 16:02:42 -0500 Subject: [PATCH] Implement removeHandler. Fix #118 --- .../ExampleSwiftApp_iOSTests.swift | 32 +++++++++++++++++++ .../WebViewJavascriptBridge.h | 1 + .../WebViewJavascriptBridge.m | 4 +++ 3 files changed, 37 insertions(+) diff --git a/Example Apps/ExampleSwiftApp-iOS/ExampleSwiftApp-iOSTests/ExampleSwiftApp_iOSTests.swift b/Example Apps/ExampleSwiftApp-iOS/ExampleSwiftApp-iOSTests/ExampleSwiftApp_iOSTests.swift index 97bda1c5..3c95aad4 100644 --- a/Example Apps/ExampleSwiftApp-iOS/ExampleSwiftApp-iOSTests/ExampleSwiftApp_iOSTests.swift +++ b/Example Apps/ExampleSwiftApp-iOS/ExampleSwiftApp-iOSTests/ExampleSwiftApp_iOSTests.swift @@ -149,6 +149,7 @@ class ExampleSwiftApp_iOSTests: XCTestCase { loadEchoSample(webView); let callbackInvoked = expectation(description: "Callback invoked") bridge.registerHandler("objcEchoToJs") { (data, responseCallback) in + XCTAssertEqual(data as! NSDictionary, ["foo":"bar"]); responseCallback!(data) } bridge.callHandler("jsRcvResponseTest", data:nil) { (responseData) in @@ -168,6 +169,7 @@ class ExampleSwiftApp_iOSTests: XCTestCase { loadEchoSample(webView); let callbackInvoked = expectation(description: "Callback invoked") bridge.registerHandler("objcEchoToJs") { (data, responseCallback) in + XCTAssertEqual(data as! NSDictionary, ["foo":"bar"]); responseCallback!(data); } bridge.callHandler("jsRcvResponseTest", data:nil) { (responseData) in @@ -176,4 +178,34 @@ class ExampleSwiftApp_iOSTests: XCTestCase { } } + func testRemoveHandler() { + _testRemoveHandler(uiWebView) + _testRemoveHandler(wkWebView) + waitForExpectations(timeout: timeout, handler: nil) + } + func _testRemoveHandler(_ webView: Any) { + loadEchoSample(webView); + let bridge = bridgeForWebView(webView) + let callbackNotInvoked = expectation(description: "Callback invoked") + var count = 0 + bridge.registerHandler("objcEchoToJs") { (data, callback) in + count += 1 + callback!(data) + } + bridge.callHandler("jsRcvResponseTest", data:nil) { (responseData) in + XCTAssertEqual(responseData as! String, "Response from JS"); + bridge.removeHandler("objcEchoToJs") + bridge.callHandler("jsRcvResponseTest", data:nil) { (responseData) in + // Since we have removed the "objcEchoToJs" handler, and since the + // echo.html javascript won't call the response callback until it has + // received a response from "objcEchoToJs", we should never get here + XCTAssert(false) + } + bridge.callHandler("echoHandler", data:nil ) { (responseData) in + XCTAssertEqual(count, 1) + callbackNotInvoked.fulfill() + } + } + } + } diff --git a/WebViewJavascriptBridge/WebViewJavascriptBridge.h b/WebViewJavascriptBridge/WebViewJavascriptBridge.h index 9790cc82..1e873be2 100755 --- a/WebViewJavascriptBridge/WebViewJavascriptBridge.h +++ b/WebViewJavascriptBridge/WebViewJavascriptBridge.h @@ -40,6 +40,7 @@ + (void)setLogMaxLength:(int)length; - (void)registerHandler:(NSString*)handlerName handler:(WVJBHandler)handler; +- (void)removeHandler:(NSString*)handlerName; - (void)callHandler:(NSString*)handlerName; - (void)callHandler:(NSString*)handlerName data:(id)data; - (void)callHandler:(NSString*)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback; diff --git a/WebViewJavascriptBridge/WebViewJavascriptBridge.m b/WebViewJavascriptBridge/WebViewJavascriptBridge.m index 292d4b0a..032190d0 100755 --- a/WebViewJavascriptBridge/WebViewJavascriptBridge.m +++ b/WebViewJavascriptBridge/WebViewJavascriptBridge.m @@ -81,6 +81,10 @@ - (void)registerHandler:(NSString *)handlerName handler:(WVJBHandler)handler { _base.messageHandlers[handlerName] = [handler copy]; } +- (void)removeHandler:(NSString *)handlerName { + [_base.messageHandlers removeObjectForKey:handlerName]; +} + - (void)disableJavscriptAlertBoxSafetyTimeout { [_base disableJavscriptAlertBoxSafetyTimeout]; }