Skip to content

Commit 18820b7

Browse files
committed
wkwebview example for osx
1 parent c376eb5 commit 18820b7

File tree

9 files changed

+3774
-0
lines changed

9 files changed

+3774
-0
lines changed

Example Apps/ExampleApp-OSX-wkwebview.xcodeproj/project.pbxproj

Lines changed: 342 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// AppDelegate.h
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 <Cocoa/Cocoa.h>
10+
11+
@interface AppDelegate : NSObject <NSApplicationDelegate>
12+
13+
@property (assign) IBOutlet NSWindow *window;
14+
15+
@end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>${EXECUTABLE_NAME}</string>
9+
<key>CFBundleIconFile</key>
10+
<string></string>
11+
<key>CFBundleIdentifier</key>
12+
<string>WebViewJavascriptBridge.${PRODUCT_NAME:rfc1034identifier}</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleName</key>
16+
<string>${PRODUCT_NAME}</string>
17+
<key>CFBundlePackageType</key>
18+
<string>APPL</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>1.0</string>
21+
<key>CFBundleSignature</key>
22+
<string>????</string>
23+
<key>CFBundleVersion</key>
24+
<string>1</string>
25+
<key>LSMinimumSystemVersion</key>
26+
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
27+
<key>NSHumanReadableCopyright</key>
28+
<string>Copyright © 2013 Marcus Westin. All rights reserved.</string>
29+
<key>NSMainNibFile</key>
30+
<string>MainMenu</string>
31+
<key>NSPrincipalClass</key>
32+
<string>NSApplication</string>
33+
</dict>
34+
</plist>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//
2+
// Prefix header for all source files of the 'ExampleApp-OSX' target in the 'ExampleApp-OSX' project
3+
//
4+
5+
#ifdef __OBJC__
6+
#import <Cocoa/Cocoa.h>
7+
#endif

0 commit comments

Comments
 (0)