You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove the error parameter from all response callbacks. This change breaks API backwards compatibility!
Previously, responses were sent using either `response.respondWith(data)` or `response.respondWithError(error)`. Response handlers were then required to take two parameters:
`
function responseHandler(error, responseData) {
if (error) { /* handle error */ }
...
}
`
However, this approach causes confusion, as seen in GH issue marcuswestin#18. Instead, we do away with the notion of errors in responses and simply have response callback blocks with a single data parameter:
`responseCallback(data)`
and
`function responseHandler(responseData) { ... }`
To migrate from old ObjC code, simple replace all instances of `WVJBResponse* response` with `WVJBResponseCallback responseCallback`, and replace all instances of `response.respondWith(data)` with `responseCallback(data)`.
To migrate from old Javascript code, replace all instances of `response.respondWith(data)` with `response(data)`.
[bridge send:@"Give me a response, will you?" responseCallback:^(id error, id responseData) {
50
-
NSLog(@"objc got its response! %@ %@", error, responseData);
48
+
[bridge send:@"Give me a response, will you?" responseCallback:^(id responseData) {
49
+
NSLog(@"ObjC got its response! %@ %@", responseData);
51
50
}];
52
51
53
52
4) Finally, set up the javascript side:
54
53
55
54
document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady(event) {
56
55
var bridge = event.bridge
57
-
bridge.init(function(message, response) {
56
+
bridge.init(function(message, responseCallback) {
58
57
alert('Received message: ' + message)
59
-
if (response) {
60
-
response.respondWith("Right back atcha")
61
-
// or use response.respondWithError("Booh!")
58
+
if (responseCallback) {
59
+
responseCallback("Right back atcha")
62
60
}
63
61
})
64
62
bridge.send('Hello from the javascript')
65
-
bridge.send('Please respond to this', function responseCallback(error, responseData) {
66
-
console.log("javascript got its response", error, responseData)
63
+
bridge.send('Please respond to this', function responseCallback(responseData) {
64
+
console.log("Javascript got its response", responseData)
67
65
})
68
66
}, false)
69
67
@@ -77,20 +75,20 @@ API Reference
77
75
78
76
Create a javascript bridge for the given UIWebView.
79
77
80
-
The `WVJBResponse` will not be `nil` if the javascript expects a response.
78
+
The `WVJBResponseCallback` will not be `nil` if the javascript expects a response.
81
79
82
80
Optionally, pass in `webViewDelegate:(UIWebViewDelegate*)webViewDelegate` if you need to respond to the [UIWebView's lifecycle events](http://developer.apple.com/library/ios/documentation/uikit/reference/UIWebViewDelegate_Protocol/Reference/Reference.html).
Register a handler called `handlerName`. The ObjC can then call this handler with `[bridge callHandler:"handlerName" data:@"Foo"]` and `[bridge callHandler:"handlerName" data:@"Foo" responseCallback:^(id error, id responseData) { ... }]`
174
+
Register a handler called `handlerName`. The ObjC can then call this handler with `[bridge callHandler:"handlerName" data:@"Foo"]` and `[bridge callHandler:"handlerName" data:@"Foo" responseCallback:^(id responseData) { ... }]`
0 commit comments