99#import " AppDelegate.h"
1010#import < WebKit/WebKit.h>
1111#import " WebViewJavascriptBridge.h"
12- #import " WKWebViewJavascriptBridge.h"
1312
1413@implementation AppDelegate {
1514 WebView * _webView;
1615 WKWebView *_WKWebView;
1716 WebViewJavascriptBridge* _bridge;
18- WKWebViewJavascriptBridge * _WKBridge;
17+ WebViewJavascriptBridge * _WKBridge;
1918 NSView * _WKWebViewWrapper;
2019}
2120
@@ -28,38 +27,24 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
2827
2928- (void )_configureWebview {
3029 // Create Bridge
31- _bridge = [WebViewJavascriptBridge bridgeForWebView: _webView handler: ^(id data, WVJBResponseCallback responseCallback) {
32- NSLog (@" ObjC received message from JS: %@ " , data);
33- responseCallback (@" Response for message from ObjC" );
34- }];
30+ _bridge = [WebViewJavascriptBridge bridgeForWebView: _webView];
3531
3632 [_bridge registerHandler: @" testObjcCallback" handler: ^(id data, WVJBResponseCallback responseCallback) {
3733 NSLog (@" testObjcCallback called: %@ " , data);
3834 responseCallback (@" Response from testObjcCallback" );
3935 }];
4036
41- [_bridge send: @" A string sent from ObjC before Webview has loaded." responseCallback: ^(id responseData) {
42- NSLog (@" objc got response! %@ " , responseData);
43- }];
44-
4537 [_bridge callHandler: @" testJavascriptHandler" data: @{ @" foo" :@" before ready" }];
4638
4739 // Create Buttons
48- NSButton *messageButton = [[NSButton alloc ] initWithFrame: NSMakeRect (5 , 0 , 120 , 40 )];
49- [messageButton setTitle: @" Send message" ];
50- [messageButton setBezelStyle: NSRoundedBezelStyle ];
51- [messageButton setTarget: self ];
52- [messageButton setAction: @selector (_sendMessage )];
53- [_webView addSubview: messageButton];
54-
55- NSButton *callbackButton = [[NSButton alloc ] initWithFrame: NSMakeRect (120 , 0 , 120 , 40 )];
40+ NSButton *callbackButton = [[NSButton alloc ] initWithFrame: NSMakeRect (5 , 0 , 120 , 40 )];
5641 [callbackButton setTitle: @" Call handler" ];
5742 [callbackButton setBezelStyle: NSRoundedBezelStyle ];
5843 [callbackButton setTarget: self ];
5944 [callbackButton setAction: @selector (_callHandler )];
6045 [_webView addSubview: callbackButton];
6146
62- NSButton *webViewToggleButton = [[NSButton alloc ] initWithFrame: NSMakeRect (235 , 0 , 180 , 40 )];
47+ NSButton *webViewToggleButton = [[NSButton alloc ] initWithFrame: NSMakeRect (120 , 0 , 180 , 40 )];
6348 [webViewToggleButton setTitle: @" Switch to WKWebView" ];
6449 [webViewToggleButton setBezelStyle: NSRoundedBezelStyle ];
6550 [webViewToggleButton setTarget: self ];
@@ -70,44 +55,31 @@ - (void)_configureWebview {
7055 // Load Page
7156 NSString * htmlPath = [[NSBundle mainBundle ] pathForResource: @" ExampleApp" ofType: @" html" ];
7257 NSString * html = [NSString stringWithContentsOfFile: htmlPath encoding: NSUTF8StringEncoding error: nil ];
73- [[_webView mainFrame ] loadHTMLString: html baseURL: nil ];
58+ NSURL *baseURL = [NSURL fileURLWithPath: htmlPath];
59+ [[_webView mainFrame ] loadHTMLString: html baseURL: baseURL];
7460}
7561
7662
7763- (void )_configureWKWebview {
7864 // Create Bridge
79- _WKBridge = [WKWebViewJavascriptBridge bridgeForWebView: _WKWebView handler: ^(id data, WVJBResponseCallback responseCallback) {
80- NSLog (@" ObjC received message from JS: %@ " , data);
81- responseCallback (@" Response for message from ObjC" );
82- }];
65+ _WKBridge = [WebViewJavascriptBridge bridgeForWebView: _WKWebView];
8366
8467 [_WKBridge registerHandler: @" testObjcCallback" handler: ^(id data, WVJBResponseCallback responseCallback) {
8568 NSLog (@" testObjcCallback called: %@ " , data);
8669 responseCallback (@" Response from testObjcCallback" );
8770 }];
8871
89- [_WKBridge send: @" A string sent from ObjC before Webview has loaded." responseCallback: ^(id responseData) {
90- NSLog (@" objc got response! %@ " , responseData);
91- }];
92-
9372 [_WKBridge callHandler: @" testJavascriptHandler" data: @{ @" foo" :@" before ready" }];
9473
9574 // Create Buttons
96- NSButton *messageButton = [[NSButton alloc ] initWithFrame: NSMakeRect (5 , 0 , 120 , 40 )];
97- [messageButton setTitle: @" Send message" ];
98- [messageButton setBezelStyle: NSRoundedBezelStyle ];
99- [messageButton setTarget: self ];
100- [messageButton setAction: @selector (_WKSendMessage )];
101- [_WKWebView addSubview: messageButton];
102-
103- NSButton *callbackButton = [[NSButton alloc ] initWithFrame: NSMakeRect (120 , 0 , 120 , 40 )];
75+ NSButton *callbackButton = [[NSButton alloc ] initWithFrame: NSMakeRect (5 , 0 , 120 , 40 )];
10476 [callbackButton setTitle: @" Call handler" ];
10577 [callbackButton setBezelStyle: NSRoundedBezelStyle ];
10678 [callbackButton setTarget: self ];
10779 [callbackButton setAction: @selector (_WKCallHandler )];
10880 [_WKWebView addSubview: callbackButton];
10981
110- NSButton *webViewToggleButton = [[NSButton alloc ] initWithFrame: NSMakeRect (235 , 0 , 180 , 40 )];
82+ NSButton *webViewToggleButton = [[NSButton alloc ] initWithFrame: NSMakeRect (120 , 0 , 180 , 40 )];
11183 [webViewToggleButton setTitle: @" Switch to WebView" ];
11284 [webViewToggleButton setBezelStyle: NSRoundedBezelStyle ];
11385 [webViewToggleButton setTarget: self ];
@@ -117,33 +89,22 @@ - (void)_configureWKWebview {
11789 // Load Page
11890 NSString * htmlPath = [[NSBundle mainBundle ] pathForResource: @" ExampleApp" ofType: @" html" ];
11991 NSString * html = [NSString stringWithContentsOfFile: htmlPath encoding: NSUTF8StringEncoding error: nil ];
120- [_WKWebView loadHTMLString: html baseURL: nil ];
92+ NSURL *baseURL = [NSURL fileURLWithPath: htmlPath];
93+ [_WKWebView loadHTMLString: html baseURL: baseURL];
12194}
12295
12396-(void )_toggleExample {
12497 _WKWebView.hidden = !_WKWebView.isHidden ;
12598 _webView.hidden = !_webView.isHidden ;
12699}
127100
128- - (void )_sendMessage {
129- [_bridge send: @" A string sent from ObjC to JS" responseCallback: ^(id response) {
130- NSLog (@" sendMessage got response: %@ " , response);
131- }];
132- }
133-
134101- (void )_callHandler {
135102 id data = @{ @" greetingFromObjC" : @" Hi there, JS!" };
136103 [_bridge callHandler: @" testJavascriptHandler" data: data responseCallback: ^(id response) {
137104 NSLog (@" testJavascriptHandler responded: %@ " , response);
138105 }];
139106}
140107
141- - (void )_WKSendMessage {
142- [_WKBridge send: @" A string sent from ObjC to JS" responseCallback: ^(id response) {
143- NSLog (@" sendMessage got response: %@ " , response);
144- }];
145- }
146-
147108- (void )_WKCallHandler {
148109 id data = @{ @" greetingFromObjC" : @" Hi there, JS!" };
149110 [_WKBridge callHandler: @" testJavascriptHandler" data: data responseCallback: ^(id response) {
0 commit comments