From 47cb1aee4a5ed9dc0cc6adacaeb2944839c36927 Mon Sep 17 00:00:00 2001 From: Marcus Westin Date: Sat, 14 Feb 2015 12:50:36 -0500 Subject: [PATCH] Clarify that the wvjb instance must be stored in the README examples Fix #116 --- README.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f36b17b0..f29e4da0 100644 --- a/README.md +++ b/README.md @@ -32,22 +32,26 @@ To use a WebViewJavascriptBridge in your own project: - In the dialog that appears, uncheck "Copy items into destination group's folder" and select "Create groups for any folders" -2) Import the header file: +2) Import the header file and declare an ivar property: #import "WebViewJavascriptBridge.h" +... + + @property WebViewJavascriptBridge* bridge; + 3) Instantiate WebViewJavascriptBridge with a UIWebView (iOS) or WebView (OSX): - WebViewJavascriptBridge* bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) { + self.bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) { NSLog(@"Received message from javascript: %@", data); responseCallback(@"Right back atcha"); }]; 4) Go ahead and send some messages from ObjC to javascript: - [bridge send:@"Well hello there"]; - [bridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]]; - [bridge send:@"Give me a response, will you?" responseCallback:^(id responseData) { + [self.bridge send:@"Well hello there"]; + [self.bridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]]; + [self.bridge send:@"Give me a response, will you?" responseCallback:^(id responseData) { NSLog(@"ObjC got its response! %@", responseData); }]; @@ -117,9 +121,9 @@ Send a message to javascript. Optionally expect a response by giving a `response Example: - [bridge send:@"Hi"]; - [bridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]]; - [bridge send:@"I expect a response!" responseCallback:^(id responseData) { + [self.bridge send:@"Hi"]; + [self.bridge send:[NSDictionary dictionaryWithObject:@"Foo" forKey:@"Bar"]]; + [self.bridge send:@"I expect a response!" responseCallback:^(id responseData) { NSLog(@"Got response! %@", responseData); }]; @@ -129,7 +133,7 @@ Register a handler called `handlerName`. The javascript can then call this handl Example: - [bridge registerHandler:@"getScreenHeight" handler:^(id data, WVJBResponseCallback responseCallback) { + [self.bridge registerHandler:@"getScreenHeight" handler:^(id data, WVJBResponseCallback responseCallback) { responseCallback([NSNumber numberWithInt:[UIScreen mainScreen].bounds.size.height]); }]; @@ -140,8 +144,8 @@ Call the javascript handler called `handlerName`. Optionally expect a response b Example: - [bridge callHandler:@"showAlert" data:@"Hi from ObjC to JS!"]; - [bridge callHandler:@"getCurrentPageUrl" data:nil responseCallback:^(id responseData) { + [self.bridge callHandler:@"showAlert" data:@"Hi from ObjC to JS!"]; + [self.bridge callHandler:@"getCurrentPageUrl" data:nil responseCallback:^(id responseData) { NSLog(@"Current UIWebView page URL is: %@", responseData); }];