Skip to content

Commit

Permalink
wkwebview example for osx
Browse files Browse the repository at this point in the history
  • Loading branch information
lokimeyburg committed Dec 4, 2014
1 parent c376eb5 commit 18820b7
Show file tree
Hide file tree
Showing 9 changed files with 3,774 additions and 0 deletions.
342 changes: 342 additions & 0 deletions Example Apps/ExampleApp-OSX-wkwebview.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions Example Apps/ExampleApp-OSX-wkwebview/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// AppDelegate.h
// ExampleApp-OSX
//
// Created by Marcus Westin on 6/8/13.
// Copyright (c) 2013 Marcus Westin. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSWindow *window;

@end
87 changes: 87 additions & 0 deletions Example Apps/ExampleApp-OSX-wkwebview/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 "WKWebViewJavascriptBridge.h"

@implementation AppDelegate {
WKWebView* _webView;
WKWebViewJavascriptBridge* _bridge;
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self _createViews];
[self _createBridge];
[self _createObjcButtons];
[self _loadPage];
}

- (void)_createBridge {
_bridge = [WKWebViewJavascriptBridge 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:@{ @"foo":@"before ready" }];
}

- (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 {
id data = @{ @"greetingFromObjC": @"Hi there, JS!" };
[_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
NSLog(@"testJavascriptHandler responded: %@", response);
}];
}

- (void)_createViews {
NSView* contentView = _window.contentView;
_webView = [[WKWebView 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 loadHTMLString:html baseURL:nil];
}


@end
34 changes: 34 additions & 0 deletions Example Apps/ExampleApp-OSX-wkwebview/ExampleApp-OSX-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>WebViewJavascriptBridge.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2013 Marcus Westin. All rights reserved.</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//
// Prefix header for all source files of the 'ExampleApp-OSX' target in the 'ExampleApp-OSX' project
//

#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#endif
Loading

0 comments on commit 18820b7

Please sign in to comment.