|
| 1 | +// |
| 2 | +// ExampleAppViewController.m |
| 3 | +// ExampleApp-iOS |
| 4 | +// |
| 5 | +// Created by Marcus Westin on 1/13/14. |
| 6 | +// Copyright (c) 2014 Marcus Westin. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "ExampleAppViewController.h" |
| 10 | +#import "WebViewJavascriptBridge.h" |
| 11 | + |
| 12 | +@interface ExampleAppViewController () |
| 13 | +@property WebViewJavascriptBridge* bridge; |
| 14 | +@end |
| 15 | + |
| 16 | +@implementation ExampleAppViewController |
| 17 | + |
| 18 | +- (void)viewWillAppear:(BOOL)animated { |
| 19 | + if (_bridge) { return; } |
| 20 | + |
| 21 | + UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; |
| 22 | + [self.view addSubview:webView]; |
| 23 | + |
| 24 | + [WebViewJavascriptBridge enableLogging]; |
| 25 | + |
| 26 | + _bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) { |
| 27 | + NSLog(@"ObjC received message from JS: %@", data); |
| 28 | + responseCallback(@"Response for message from ObjC"); |
| 29 | + }]; |
| 30 | + |
| 31 | + [_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) { |
| 32 | + NSLog(@"testObjcCallback called: %@", data); |
| 33 | + responseCallback(@"Response from testObjcCallback"); |
| 34 | + }]; |
| 35 | + |
| 36 | + [_bridge send:@"A string sent from ObjC before Webview has loaded." responseCallback:^(id responseData) { |
| 37 | + NSLog(@"objc got response! %@", responseData); |
| 38 | + }]; |
| 39 | + |
| 40 | + [_bridge callHandler:@"testJavascriptHandler" data:@{ @"foo":@"before ready" }]; |
| 41 | + |
| 42 | + [self renderButtons:webView]; |
| 43 | + [self loadExamplePage:webView]; |
| 44 | + |
| 45 | + [_bridge send:@"A string sent from ObjC after Webview has loaded."]; |
| 46 | +} |
| 47 | + |
| 48 | +- (void)webViewDidStartLoad:(UIWebView *)webView { |
| 49 | + NSLog(@"webViewDidStartLoad"); |
| 50 | +} |
| 51 | + |
| 52 | +- (void)webViewDidFinishLoad:(UIWebView *)webView { |
| 53 | + NSLog(@"webViewDidFinishLoad"); |
| 54 | +} |
| 55 | + |
| 56 | +- (void)renderButtons:(UIWebView*)webView { |
| 57 | + UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
| 58 | + [messageButton setTitle:@"Send message" forState:UIControlStateNormal]; |
| 59 | + [messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside]; |
| 60 | + [self.view insertSubview:messageButton aboveSubview:webView]; |
| 61 | + messageButton.frame = CGRectMake(20, 414, 130, 45); |
| 62 | + |
| 63 | + UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; |
| 64 | + [callbackButton setTitle:@"Call handler" forState:UIControlStateNormal]; |
| 65 | + [callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside]; |
| 66 | + [self.view insertSubview:callbackButton aboveSubview:webView]; |
| 67 | + callbackButton.frame = CGRectMake(170, 414, 130, 45); |
| 68 | +} |
| 69 | + |
| 70 | +- (void)sendMessage:(id)sender { |
| 71 | + [_bridge send:@"A string sent from ObjC to JS" responseCallback:^(id response) { |
| 72 | + NSLog(@"sendMessage got response: %@", response); |
| 73 | + }]; |
| 74 | +} |
| 75 | + |
| 76 | +- (void)callHandler:(id)sender { |
| 77 | + id data = @{ @"greetingFromObjC": @"Hi there, JS!" }; |
| 78 | + [_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) { |
| 79 | + NSLog(@"testJavascriptHandler responded: %@", response); |
| 80 | + }]; |
| 81 | +} |
| 82 | + |
| 83 | +- (void)loadExamplePage:(UIWebView*)webView { |
| 84 | + NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"]; |
| 85 | + NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil]; |
| 86 | + [webView loadHTMLString:appHtml baseURL:nil]; |
| 87 | +} |
| 88 | +@end |
0 commit comments