Skip to content

Commit 47cb1ae

Browse files
committed
Clarify that the wvjb instance must be stored in the README examples
Fix marcuswestin#116
1 parent 52b9134 commit 47cb1ae

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,26 @@ To use a WebViewJavascriptBridge in your own project:
3232

3333
- In the dialog that appears, uncheck "Copy items into destination group's folder" and select "Create groups for any folders"
3434

35-
2) Import the header file:
35+
2) Import the header file and declare an ivar property:
3636

3737
#import "WebViewJavascriptBridge.h"
3838

39+
...
40+
41+
@property WebViewJavascriptBridge* bridge;
42+
3943
3) Instantiate WebViewJavascriptBridge with a UIWebView (iOS) or WebView (OSX):
4044

41-
WebViewJavascriptBridge* bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) {
45+
self.bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) {
4246
NSLog(@"Received message from javascript: %@", data);
4347
responseCallback(@"Right back atcha");
4448
}];
4549

4650
4) Go ahead and send some messages from ObjC to javascript:
4751

48-
[bridge send:@"Well hello there"];
49-
[bridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]];
50-
[bridge send:@"Give me a response, will you?" responseCallback:^(id responseData) {
52+
[self.bridge send:@"Well hello there"];
53+
[self.bridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]];
54+
[self.bridge send:@"Give me a response, will you?" responseCallback:^(id responseData) {
5155
NSLog(@"ObjC got its response! %@", responseData);
5256
}];
5357

@@ -117,9 +121,9 @@ Send a message to javascript. Optionally expect a response by giving a `response
117121

118122
Example:
119123

120-
[bridge send:@"Hi"];
121-
[bridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]];
122-
[bridge send:@"I expect a response!" responseCallback:^(id responseData) {
124+
[self.bridge send:@"Hi"];
125+
[self.bridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]];
126+
[self.bridge send:@"I expect a response!" responseCallback:^(id responseData) {
123127
NSLog(@"Got response! %@", responseData);
124128
}];
125129

@@ -129,7 +133,7 @@ Register a handler called `handlerName`. The javascript can then call this handl
129133

130134
Example:
131135

132-
[bridge registerHandler:@"getScreenHeight" handler:^(id data, WVJBResponseCallback responseCallback) {
136+
[self.bridge registerHandler:@"getScreenHeight" handler:^(id data, WVJBResponseCallback responseCallback) {
133137
responseCallback([NSNumber numberWithInt:[UIScreen mainScreen].bounds.size.height]);
134138
}];
135139

@@ -140,8 +144,8 @@ Call the javascript handler called `handlerName`. Optionally expect a response b
140144

141145
Example:
142146

143-
[bridge callHandler:@"showAlert" data:@"Hi from ObjC to JS!"];
144-
[bridge callHandler:@"getCurrentPageUrl" data:nil responseCallback:^(id responseData) {
147+
[self.bridge callHandler:@"showAlert" data:@"Hi from ObjC to JS!"];
148+
[self.bridge callHandler:@"getCurrentPageUrl" data:nil responseCallback:^(id responseData) {
145149
NSLog(@"Current UIWebView page URL is: %@", responseData);
146150
}];
147151

0 commit comments

Comments
 (0)