forked from marcuswestin/WebViewJavascriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c376eb5
commit 18820b7
Showing
9 changed files
with
3,774 additions
and
0 deletions.
There are no files selected for viewing
342 changes: 342 additions & 0 deletions
342
Example Apps/ExampleApp-OSX-wkwebview.xcodeproj/project.pbxproj
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
Example Apps/ExampleApp-OSX-wkwebview/ExampleApp-OSX-Info.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
7 changes: 7 additions & 0 deletions
7
Example Apps/ExampleApp-OSX-wkwebview/ExampleApp-OSX-Prefix.pch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.