@@ -32,22 +32,26 @@ To use a WebViewJavascriptBridge in your own project:
32
32
33
33
- In the dialog that appears, uncheck "Copy items into destination group's folder" and select "Create groups for any folders"
34
34
35
- 2 ) Import the header file:
35
+ 2 ) Import the header file and declare an ivar property :
36
36
37
37
#import "WebViewJavascriptBridge.h"
38
38
39
+ ...
40
+
41
+ @property WebViewJavascriptBridge* bridge;
42
+
39
43
3 ) Instantiate WebViewJavascriptBridge with a UIWebView (iOS) or WebView (OSX):
40
44
41
- WebViewJavascriptBridge* bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) {
45
+ self. bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) {
42
46
NSLog(@"Received message from javascript: %@", data);
43
47
responseCallback(@"Right back atcha");
44
48
}];
45
49
46
50
4 ) Go ahead and send some messages from ObjC to javascript:
47
51
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) {
51
55
NSLog(@"ObjC got its response! %@", responseData);
52
56
}];
53
57
@@ -117,9 +121,9 @@ Send a message to javascript. Optionally expect a response by giving a `response
117
121
118
122
Example:
119
123
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) {
123
127
NSLog(@"Got response! %@", responseData);
124
128
}];
125
129
@@ -129,7 +133,7 @@ Register a handler called `handlerName`. The javascript can then call this handl
129
133
130
134
Example:
131
135
132
- [bridge registerHandler:@"getScreenHeight" handler:^(id data, WVJBResponseCallback responseCallback) {
136
+ [self. bridge registerHandler:@"getScreenHeight" handler:^(id data, WVJBResponseCallback responseCallback) {
133
137
responseCallback([NSNumber numberWithInt:[UIScreen mainScreen].bounds.size.height]);
134
138
}];
135
139
@@ -140,8 +144,8 @@ Call the javascript handler called `handlerName`. Optionally expect a response b
140
144
141
145
Example:
142
146
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) {
145
149
NSLog(@"Current UIWebView page URL is: %@", responseData);
146
150
}];
147
151
0 commit comments