|
| 1 | +// |
| 2 | +// AppDelegate.m |
| 3 | +// ExampleApp-OSX |
| 4 | +// |
| 5 | +// Created by Marcus Westin on 6/8/13. |
| 6 | +// Copyright (c) 2013 Marcus Westin. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "AppDelegate.h" |
| 10 | +#import <WebKit/WebKit.h> |
| 11 | +#import "WKWebViewJavascriptBridge.h" |
| 12 | + |
| 13 | +@implementation AppDelegate { |
| 14 | + WKWebView* _webView; |
| 15 | + WKWebViewJavascriptBridge* _bridge; |
| 16 | +} |
| 17 | + |
| 18 | +- (void)applicationDidFinishLaunching:(NSNotification *)aNotification |
| 19 | +{ |
| 20 | + [self _createViews]; |
| 21 | + [self _createBridge]; |
| 22 | + [self _createObjcButtons]; |
| 23 | + [self _loadPage]; |
| 24 | +} |
| 25 | + |
| 26 | +- (void)_createBridge { |
| 27 | + _bridge = [WKWebViewJavascriptBridge bridgeForWebView:_webView handler:^(id data, WVJBResponseCallback responseCallback) { |
| 28 | + NSLog(@"ObjC received message from JS: %@", data); |
| 29 | + responseCallback(@"Response for message from ObjC"); |
| 30 | + }]; |
| 31 | + |
| 32 | + [_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) { |
| 33 | + NSLog(@"testObjcCallback called: %@", data); |
| 34 | + responseCallback(@"Response from testObjcCallback"); |
| 35 | + }]; |
| 36 | + |
| 37 | + [_bridge send:@"A string sent from ObjC before Webview has loaded." responseCallback:^(id responseData) { |
| 38 | + NSLog(@"objc got response! %@", responseData); |
| 39 | + }]; |
| 40 | + |
| 41 | + [_bridge callHandler:@"testJavascriptHandler" data:@{ @"foo":@"before ready" }]; |
| 42 | +} |
| 43 | + |
| 44 | +- (void)_createObjcButtons { |
| 45 | + NSButton *messageButton = [[NSButton alloc] initWithFrame:NSMakeRect(5, 0, 120, 40)]; |
| 46 | + [messageButton setTitle:@"Send message"]; |
| 47 | + [messageButton setBezelStyle:NSRoundedBezelStyle]; |
| 48 | + [messageButton setTarget:self]; |
| 49 | + [messageButton setAction:@selector(_sendMessage)]; |
| 50 | + [_webView addSubview:messageButton]; |
| 51 | + |
| 52 | + NSButton *callbackButton = [[NSButton alloc] initWithFrame:NSMakeRect(120, 0, 120, 40)]; |
| 53 | + [callbackButton setTitle:@"Call handler"]; |
| 54 | + [callbackButton setBezelStyle:NSRoundedBezelStyle]; |
| 55 | + [callbackButton setTarget:self]; |
| 56 | + [callbackButton setAction:@selector(_callHandler)]; |
| 57 | + [_webView addSubview:callbackButton]; |
| 58 | +} |
| 59 | + |
| 60 | +- (void)_sendMessage { |
| 61 | + [_bridge send:@"A string sent from ObjC to JS" responseCallback:^(id response) { |
| 62 | + NSLog(@"sendMessage got response: %@", response); |
| 63 | + }]; |
| 64 | +} |
| 65 | + |
| 66 | +- (void)_callHandler { |
| 67 | + id data = @{ @"greetingFromObjC": @"Hi there, JS!" }; |
| 68 | + [_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) { |
| 69 | + NSLog(@"testJavascriptHandler responded: %@", response); |
| 70 | + }]; |
| 71 | +} |
| 72 | + |
| 73 | +- (void)_createViews { |
| 74 | + NSView* contentView = _window.contentView; |
| 75 | + _webView = [[WKWebView alloc] initWithFrame:contentView.frame]; |
| 76 | + [_webView setAutoresizingMask:(NSViewHeightSizable | NSViewWidthSizable)]; |
| 77 | + [contentView addSubview:_webView]; |
| 78 | +} |
| 79 | + |
| 80 | +- (void)_loadPage { |
| 81 | + NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"]; |
| 82 | + NSString* html = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil]; |
| 83 | + [_webView loadHTMLString:html baseURL:nil]; |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +@end |
0 commit comments