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