Skip to content

Commit 0b7c995

Browse files
committed
Implement removeHandler. Fix marcuswestin#118
1 parent 690ae4d commit 0b7c995

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Example Apps/ExampleSwiftApp-iOS/ExampleSwiftApp-iOSTests/ExampleSwiftApp_iOSTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ class ExampleSwiftApp_iOSTests: XCTestCase {
149149
loadEchoSample(webView);
150150
let callbackInvoked = expectation(description: "Callback invoked")
151151
bridge.registerHandler("objcEchoToJs") { (data, responseCallback) in
152+
XCTAssertEqual(data as! NSDictionary, ["foo":"bar"]);
152153
responseCallback!(data)
153154
}
154155
bridge.callHandler("jsRcvResponseTest", data:nil) { (responseData) in
@@ -168,6 +169,7 @@ class ExampleSwiftApp_iOSTests: XCTestCase {
168169
loadEchoSample(webView);
169170
let callbackInvoked = expectation(description: "Callback invoked")
170171
bridge.registerHandler("objcEchoToJs") { (data, responseCallback) in
172+
XCTAssertEqual(data as! NSDictionary, ["foo":"bar"]);
171173
responseCallback!(data);
172174
}
173175
bridge.callHandler("jsRcvResponseTest", data:nil) { (responseData) in
@@ -176,4 +178,34 @@ class ExampleSwiftApp_iOSTests: XCTestCase {
176178
}
177179
}
178180

181+
func testRemoveHandler() {
182+
_testRemoveHandler(uiWebView)
183+
_testRemoveHandler(wkWebView)
184+
waitForExpectations(timeout: timeout, handler: nil)
185+
}
186+
func _testRemoveHandler(_ webView: Any) {
187+
loadEchoSample(webView);
188+
let bridge = bridgeForWebView(webView)
189+
let callbackNotInvoked = expectation(description: "Callback invoked")
190+
var count = 0
191+
bridge.registerHandler("objcEchoToJs") { (data, callback) in
192+
count += 1
193+
callback!(data)
194+
}
195+
bridge.callHandler("jsRcvResponseTest", data:nil) { (responseData) in
196+
XCTAssertEqual(responseData as! String, "Response from JS");
197+
bridge.removeHandler("objcEchoToJs")
198+
bridge.callHandler("jsRcvResponseTest", data:nil) { (responseData) in
199+
// Since we have removed the "objcEchoToJs" handler, and since the
200+
// echo.html javascript won't call the response callback until it has
201+
// received a response from "objcEchoToJs", we should never get here
202+
XCTAssert(false)
203+
}
204+
bridge.callHandler("echoHandler", data:nil ) { (responseData) in
205+
XCTAssertEqual(count, 1)
206+
callbackNotInvoked.fulfill()
207+
}
208+
}
209+
}
210+
179211
}

WebViewJavascriptBridge/WebViewJavascriptBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
+ (void)setLogMaxLength:(int)length;
4141

4242
- (void)registerHandler:(NSString*)handlerName handler:(WVJBHandler)handler;
43+
- (void)removeHandler:(NSString*)handlerName;
4344
- (void)callHandler:(NSString*)handlerName;
4445
- (void)callHandler:(NSString*)handlerName data:(id)data;
4546
- (void)callHandler:(NSString*)handlerName data:(id)data responseCallback:(WVJBResponseCallback)responseCallback;

WebViewJavascriptBridge/WebViewJavascriptBridge.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ - (void)registerHandler:(NSString *)handlerName handler:(WVJBHandler)handler {
8181
_base.messageHandlers[handlerName] = [handler copy];
8282
}
8383

84+
- (void)removeHandler:(NSString *)handlerName {
85+
[_base.messageHandlers removeObjectForKey:handlerName];
86+
}
87+
8488
- (void)disableJavscriptAlertBoxSafetyTimeout {
8589
[_base disableJavscriptAlertBoxSafetyTimeout];
8690
}

0 commit comments

Comments
 (0)