Skip to content

Commit cd14b61

Browse files
committed
Update JS API to pass a reference to the bridge in WebViewJavascriptBridgeReady handler. Prune documentation a bit for clarity.
1 parent 8803705 commit cd14b61

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

README.md

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,16 @@ WebViewJavascriptBridge
33

44
An iOS bridge for sending messages to and from javascript in a UIWebView.
55

6-
Getting started
7-
---------------
6+
Setup & Examples
7+
----------------
88

9-
Just open the Xcode project and hit run to see the example application.
9+
Just open the Xcode project and hit run to see ExampleApp run.
1010

11-
Setup your project
12-
------------------
13-
14-
See ExampleApp/* for example code. To use it in your own project:
11+
To use a WebViewJavascriptBridge in your own project:
1512

1613
1) Drag the `WebViewJavascriptBridge` folder into your project.
1714

18-
In the dialog that appears:
19-
- Uncheck "Copy items into destination group's folder (if needed)"
20-
- Select "Create groups for any folders"
15+
(In the dialog, uncheck "Copy items into destination group's folder" and select "Create groups for any folders")
2116

2217
2) Import the header file:
2318

@@ -30,33 +25,26 @@ In the dialog that appears:
3025
NSLog(@"Received message from javascript: %@", data);
3126
}];
3227

33-
4) Go ahead and send some messages from Objc to javascript:
28+
4) Go ahead and send some messages from ObjC to javascript:
3429

3530
[javascriptBridge send:@"Well hello there"];
3631
[javascriptBridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]];
3732
[javascriptBridge send:@"Give me a response, will you?" responseCallback:^(id responseData) {
3833
NSLog(@"I got a response! %@", responseData);
3934
}];
4035

41-
4) Finally, set up the javascript side of things:
36+
4) Finally, set up the javascript side:
4237

43-
document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady() {
44-
WebViewJavascriptBridge.init(function(message, responseCallback) {
38+
document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady(bridge) {
39+
bridge.init(function(message, responseCallback) {
4540
alert('Received message: ' + message)
4641
if (responseCallback) {
4742
responseCallback("Right back atcha")
4843
}
4944
})
50-
WebViewJavascriptBridge.send('Hello from the javascript')
45+
bridge.send('Hello from the javascript')
5146
}, false)
5247

53-
iOS4 support (with JSONKit)
54-
---------------------------
55-
56-
*Note*: iOS4 support has not yet been tested in v2.
57-
58-
WebViewJavascriptBridge uses `NSJSONSerialization` by default. If you need iOS 4 support then you can use [JSONKit](https://github.com/johnezang/JSONKit/), and add `USE_JSONKIT` to the preprocessor macros for your project.
59-
6048
API Reference
6149
-------------
6250

@@ -121,57 +109,66 @@ Example:
121109

122110
### Javascript
123111

124-
##### `document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReadyHandler() { ... }, false)`
112+
##### `document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady(bridge) { ... }, false)`
125113

126-
Always wait for the `WebViewJavascriptBridgeReady` DOM event before using `WebViewJavascriptBridge`.
114+
Always wait for the `WebViewJavascriptBridgeReady` DOM event.
127115

128116
Example:
129117

130-
document.addEventListener('WebViewJavascriptBridgeReady', function() {
131-
// Start using WebViewJavascriptBridge
118+
document.addEventListener('WebViewJavascriptBridgeReady', function(bridge) {
119+
// Start using the bridge
132120
}, false)
133121

134-
##### `WebViewJavascriptBridge.init(function messageHandler(data, responseCallback) { ... })`
122+
##### `bridge.init(function messageHandler(data, responseCallback) { ... })`
135123

136-
Initialize the WebViewJavascriptBridge. This should be called inside of the `'WebViewJavascriptBridgeReady'` event handler.
124+
Initialize the bridge. This should be called inside of the `'WebViewJavascriptBridgeReady'` event handler.
137125

138126
The `messageHandler` function will receive all messages sent from ObjC via `[bridge send:(id)data]` and `[bridge send:(id)data responseCallback:(WVJBResponseCallback)responseCallback]`.
139127

140128
The `responseCallback` will be a function if ObjC sent the message with a `WVJBResponseCallback` block, or `undefined` otherwise.
141129

142130
Example:
143131

144-
WebViewJavascriptBridge.init(function(data, responseCallback) {
132+
bridge.init(function(data, responseCallback) {
145133
alert("Got data " + JSON.stringify(data))
146134
if (responseCallback) {
147135
responseCallback("Right back atcha!")
148136
}
149137
})
150138

151-
##### `WebViewJavascriptBridge.send("Hi there!")`
152-
##### `WebViewJavascriptBridge.send({ Foo:"Bar" })`
153-
##### `WebViewJavascriptBridge.send(data, function responseCallback(responseData) { ... })`
139+
##### `bridge.send("Hi there!")`
140+
##### `bridge.send({ Foo:"Bar" })`
141+
##### `bridge.send(data, function responseCallback(responseData) { ... })`
154142

155143
Send a message to ObjC. Optionally expect a response by giving a `responseCallback` function.
156144

157145
Example:
158146

159-
WebViewJavascriptBridge.send("Hi there!")
160-
WebViewJavascriptBridge.send("Hi there!", function(response) {
147+
bridge.send("Hi there!")
148+
bridge.send("Hi there!", function(response) {
161149
alert("I got a response! "+JSON.stringify(response))
162150
})
163151

164-
##### `WebViewJavascriptBridge.registerHandler("handlerName", function(data, responseCallback) { ... })`
152+
##### `WebViewJavascrbridgeiptBridge.registerHandler("handlerName", function(data, responseCallback) { ... })`
165153

166154
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) { ... }]`
167155

168156
Example:
169157

170-
WebViewJavascriptBridge.registerHandler("showAlert", function(data) { alert(data) })
171-
WebViewJavascriptBridge.registerHandler("getCurrentPageUrl", function(data, responseCallback) {
158+
bridge.registerHandler("showAlert", function(data) { alert(data) })
159+
bridge.registerHandler("getCurrentPageUrl", function(data, responseCallback) {
172160
responseCallback(document.location.toString())
173161
})
174162

163+
164+
iOS4 support (with JSONKit)
165+
---------------------------
166+
167+
*Note*: iOS4 support has not yet been tested in v2.
168+
169+
WebViewJavascriptBridge uses `NSJSONSerialization` by default. If you need iOS 4 support then you can use [JSONKit](https://github.com/johnezang/JSONKit/), and add `USE_JSONKIT` to the preprocessor macros for your project.
170+
171+
175172
Contributors
176173
------------
177174

0 commit comments

Comments
 (0)