Skip to content

Commit

Permalink
Implement removeHandler. Fix #118
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuswestin committed Dec 27, 2016
1 parent 690ae4d commit 0b7c995
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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()
}
}
}

}
1 change: 1 addition & 0 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions WebViewJavascriptBridge/WebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down

0 comments on commit 0b7c995

Please sign in to comment.