Skip to content

Commit

Permalink
You can toggle between UIWebView and WKWebView in the example app
Browse files Browse the repository at this point in the history
  • Loading branch information
lokimeyburg committed Nov 8, 2014
1 parent 589dc2a commit 7382589
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 81 deletions.
18 changes: 12 additions & 6 deletions Example Apps/ExampleApp-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
/* Begin PBXBuildFile section */
0E50601C1A01B442000BEEEA /* WebViewJavascriptBridgeBase.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E50601B1A01B442000BEEEA /* WebViewJavascriptBridgeBase.m */; };
0E8082DB19EDC32300479452 /* WKWebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E8082DA19EDC32300479452 /* WKWebViewJavascriptBridge.m */; };
0ECB01441A0EE1F20037FF4E /* ExampleWKWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ECB01431A0EE1F20037FF4E /* ExampleWKWebViewController.m */; };
2C1562B5176B9F8400B4AE50 /* WebViewJavascriptBridge.js.txt in Resources */ = {isa = PBXBuildFile; fileRef = 2C1562B4176B9F8400B4AE50 /* WebViewJavascriptBridge.js.txt */; };
2C1562C0176BA63500B4AE50 /* WebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C1562A9176B9F6200B4AE50 /* WebViewJavascriptBridge.m */; };
2C45CA2C1884AD520002A4E2 /* ExampleAppViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C45CA2B1884AD520002A4E2 /* ExampleAppViewController.m */; };
2C45CA2C1884AD520002A4E2 /* ExampleUIWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C45CA2B1884AD520002A4E2 /* ExampleUIWebViewController.m */; };
2CA045BF17117439006DEE8B /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 2CA045B717117439006DEE8B /* InfoPlist.strings */; };
2CA045C217117439006DEE8B /* ExampleAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2CA045BD17117439006DEE8B /* ExampleAppDelegate.m */; };
2CA045C317117439006DEE8B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2CA045BE17117439006DEE8B /* main.m */; };
Expand All @@ -28,11 +29,13 @@
0E8082D919EDC32300479452 /* WKWebViewJavascriptBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKWebViewJavascriptBridge.h; sourceTree = "<group>"; };
0E8082DA19EDC32300479452 /* WKWebViewJavascriptBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WKWebViewJavascriptBridge.m; sourceTree = "<group>"; };
0E8082DC19EDD98700479452 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
0ECB01421A0EE1BA0037FF4E /* ExampleWKWebViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ExampleWKWebViewController.h; sourceTree = "<group>"; };
0ECB01431A0EE1F20037FF4E /* ExampleWKWebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleWKWebViewController.m; sourceTree = "<group>"; };
2C1562A8176B9F6200B4AE50 /* WebViewJavascriptBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebViewJavascriptBridge.h; sourceTree = "<group>"; };
2C1562A9176B9F6200B4AE50 /* WebViewJavascriptBridge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WebViewJavascriptBridge.m; sourceTree = "<group>"; };
2C1562B4176B9F8400B4AE50 /* WebViewJavascriptBridge.js.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = WebViewJavascriptBridge.js.txt; sourceTree = "<group>"; };
2C45CA2A1884AD520002A4E2 /* ExampleAppViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExampleAppViewController.h; sourceTree = "<group>"; };
2C45CA2B1884AD520002A4E2 /* ExampleAppViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleAppViewController.m; sourceTree = "<group>"; };
2C45CA2A1884AD520002A4E2 /* ExampleUIWebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExampleUIWebViewController.h; sourceTree = "<group>"; };
2C45CA2B1884AD520002A4E2 /* ExampleUIWebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleUIWebViewController.m; sourceTree = "<group>"; };
2CA045B817117439006DEE8B /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
2CA045B917117439006DEE8B /* ExampleApp-iOS-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "ExampleApp-iOS-Info.plist"; sourceTree = "<group>"; };
2CA045BA17117439006DEE8B /* ExampleApp-iOS-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ExampleApp-iOS-Prefix.pch"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -82,8 +85,10 @@
2CA0465B1711AC8D006DEE8B /* ExampleApp.html */,
2CA045BC17117439006DEE8B /* ExampleAppDelegate.h */,
2CA045BD17117439006DEE8B /* ExampleAppDelegate.m */,
2C45CA2A1884AD520002A4E2 /* ExampleAppViewController.h */,
2C45CA2B1884AD520002A4E2 /* ExampleAppViewController.m */,
2C45CA2A1884AD520002A4E2 /* ExampleUIWebViewController.h */,
2C45CA2B1884AD520002A4E2 /* ExampleUIWebViewController.m */,
0ECB01421A0EE1BA0037FF4E /* ExampleWKWebViewController.h */,
0ECB01431A0EE1F20037FF4E /* ExampleWKWebViewController.m */,
2C1562A7176B9F5400B4AE50 /* WebViewJavascriptBridge */,
2CA046211711A94E006DEE8B /* Supporting Files */,
);
Expand Down Expand Up @@ -197,7 +202,8 @@
files = (
2C1562C0176BA63500B4AE50 /* WebViewJavascriptBridge.m in Sources */,
0E8082DB19EDC32300479452 /* WKWebViewJavascriptBridge.m in Sources */,
2C45CA2C1884AD520002A4E2 /* ExampleAppViewController.m in Sources */,
2C45CA2C1884AD520002A4E2 /* ExampleUIWebViewController.m in Sources */,
0ECB01441A0EE1F20037FF4E /* ExampleWKWebViewController.m in Sources */,
2CA045C217117439006DEE8B /* ExampleAppDelegate.m in Sources */,
0E50601C1A01B442000BEEEA /* WebViewJavascriptBridgeBase.m in Sources */,
2CA045C317117439006DEE8B /* main.m in Sources */,
Expand Down
18 changes: 16 additions & 2 deletions Example Apps/ExampleApp-iOS/ExampleAppDelegate.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
#import "ExampleAppDelegate.h"
#import "ExampleAppViewController.h"
#import "ExampleUIWebViewController.h"
#import "ExampleWKWebViewController.h"

@implementation ExampleAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

ExampleUIWebViewController* UIWebViewExampleController = [[ExampleUIWebViewController alloc] init];
UIWebViewExampleController.tabBarItem.title = @"UIWebView";

ExampleWKWebViewController* WKWebViewExampleController = [[ExampleWKWebViewController alloc] init];
WKWebViewExampleController.tabBarItem.title = @"WKWebView";

UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController addChildViewController:UIWebViewExampleController];
[tabBarController addChildViewController:WKWebViewExampleController];



self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [ExampleAppViewController new];
self.window.rootViewController = tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Expand Down
32 changes: 0 additions & 32 deletions Example Apps/ExampleApp-iOS/ExampleAppViewController.h

This file was deleted.

13 changes: 13 additions & 0 deletions Example Apps/ExampleApp-iOS/ExampleUIWebViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// ExampleAppViewController.h
// ExampleApp-iOS
//
// Created by Marcus Westin on 1/13/14.
// Copyright (c) 2014 Marcus Westin. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ExampleUIWebViewController : UINavigationController <UIWebViewDelegate>

@end
101 changes: 101 additions & 0 deletions Example Apps/ExampleApp-iOS/ExampleUIWebViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// ExampleAppViewController.m
// ExampleApp-iOS
//
// Created by Marcus Westin on 1/13/14.
// Copyright (c) 2014 Marcus Westin. All rights reserved.
//

#import "ExampleUIWebViewController.h"
#import "WebViewJavascriptBridge.h"

@interface ExampleUIWebViewController ()
@property WebViewJavascriptBridge* bridge;
@end

@implementation ExampleUIWebViewController

- (void)viewWillAppear:(BOOL)animated {
if (_bridge) { return; }

UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];

[WebViewJavascriptBridge enableLogging];

_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self 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" }];

[self renderButtons:webView];
[self loadExamplePage:webView];

[_bridge send:@"A string sent from ObjC after Webview has loaded."];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(@"webViewDidStartLoad");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"webViewDidFinishLoad");
}

- (void)renderButtons:(UIWebView*)webView {
UIFont* font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];

UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[messageButton setTitle:@"Send message" forState:UIControlStateNormal];
[messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:messageButton aboveSubview:webView];
messageButton.frame = CGRectMake(10, 414, 100, 35);
messageButton.titleLabel.font = font;
messageButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.75];

UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[callbackButton setTitle:@"Call handler" forState:UIControlStateNormal];
[callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:callbackButton aboveSubview:webView];
callbackButton.frame = CGRectMake(110, 414, 100, 35);
callbackButton.titleLabel.font = font;

UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[reloadButton setTitle:@"Reload webview" forState:UIControlStateNormal];
[reloadButton addTarget:webView action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:reloadButton aboveSubview:webView];
reloadButton.frame = CGRectMake(210, 414, 100, 35);
reloadButton.titleLabel.font = font;
}

- (void)sendMessage:(id)sender {
[_bridge send:@"A string sent from ObjC to JS" responseCallback:^(id response) {
NSLog(@"sendMessage got response: %@", response);
}];
}

- (void)callHandler:(id)sender {
id data = @{ @"greetingFromObjC": @"Hi there, JS!" };
[_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
NSLog(@"testJavascriptHandler responded: %@", response);
}];
}

- (void)loadExamplePage:(UIWebView*)webView {
NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
[webView loadHTMLString:appHtml baseURL:baseURL];
}
@end
14 changes: 14 additions & 0 deletions Example Apps/ExampleApp-iOS/ExampleWKWebViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// ExampleAppViewController.h
// ExampleApp-iOS
//
// Created by Marcus Westin on 1/13/14.
// Copyright (c) 2014 Marcus Westin. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>

@interface ExampleWKWebViewController : UINavigationController<WKNavigationDelegate>

@end
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,30 @@
// Copyright (c) 2014 Marcus Westin. All rights reserved.
//

#import "ExampleAppViewController.h"

#if defined(exampleSupportsWKWebKit)
#import "ExampleWKWebViewController.h"
#import "WKWebViewJavascriptBridge.h"
# else
#import "WebViewJavascriptBridge.h"
#endif

@interface ExampleAppViewController ()
@interface ExampleWKWebViewController ()

@property EXAMPLE_BRIDGE_TYPE* bridge;
@property WKWebViewJavascriptBridge* bridge;

@end

@implementation ExampleAppViewController
@implementation ExampleWKWebViewController

- (void)viewWillAppear:(BOOL)animated {
if (_bridge) { return; }

#if defined(exampleSupportsWKWebKit)
WKWebView* webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:self.view.bounds];
webView.navigationDelegate = self;
[self.view addSubview:webView];
[WKWebViewJavascriptBridge enableLogging];
_bridge = [WKWebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC received message from JS: %@", data);
responseCallback(@"Response for message from ObjC");
}];
#else
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
[WebViewJavascriptBridge enableLogging];
_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC received message from JS: %@", data);
responseCallback(@"Response for message from ObjC");
}];
#endif
WKWebView* webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:self.view.bounds];
webView.navigationDelegate = self;
[self.view addSubview:webView];
[WKWebViewJavascriptBridge enableLogging];
_bridge = [WKWebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self 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);
Expand All @@ -71,29 +56,29 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"webViewDidFinishLoad");
}

- (void)renderButtons:(EXAMPLE_WEBVIEW_TYPE*)webView {
- (void)renderButtons:(WKWebView*)webView {
UIFont* font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];

UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[messageButton setTitle:@"Send message" forState:UIControlStateNormal];
[messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:messageButton aboveSubview:webView];
messageButton.frame = CGRectMake(10, 414, 100, 35);
[messageButton setTitle:@"Send message" forState:UIControlStateNormal];
[messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:messageButton aboveSubview:webView];
messageButton.frame = CGRectMake(10, 414, 100, 35);
messageButton.titleLabel.font = font;
messageButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.75];

UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[callbackButton setTitle:@"Call handler" forState:UIControlStateNormal];
[callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:callbackButton aboveSubview:webView];
callbackButton.frame = CGRectMake(110, 414, 100, 35);
callbackButton.frame = CGRectMake(110, 414, 100, 35);
callbackButton.titleLabel.font = font;

UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[reloadButton setTitle:@"Reload webview" forState:UIControlStateNormal];
[reloadButton addTarget:webView action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:reloadButton aboveSubview:webView];
reloadButton.frame = CGRectMake(210, 414, 100, 35);
reloadButton.frame = CGRectMake(210, 414, 100, 35);
reloadButton.titleLabel.font = font;
}

Expand All @@ -110,7 +95,7 @@ - (void)callHandler:(id)sender {
}];
}

- (void)loadExamplePage:(EXAMPLE_WEBVIEW_TYPE*)webView {
- (void)loadExamplePage:(WKWebView*)webView {
NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
Expand Down
5 changes: 2 additions & 3 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ + (instancetype)bridgeForWebView:(WKWebView*)webView webViewDelegate:(NSObject<W
+ (instancetype)bridgeForWebView:(WKWebView*)webView webViewDelegate:(NSObject<WKNavigationDelegate>*)webViewDelegate handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle
{
WKWebViewJavascriptBridge* bridge = [[WKWebViewJavascriptBridge alloc] init];
[bridge _platformSpecificSetup:webView webViewDelegate:webViewDelegate handler:messageHandler resourceBundle:bundle];
[bridge _setupInstance:webView webViewDelegate:webViewDelegate handler:messageHandler resourceBundle:bundle];
[bridge reset];
return bridge;
}
Expand Down Expand Up @@ -81,7 +81,7 @@ - (void)dealloc {
/* WKWebView Specific Internals
******************************/

- (void) _platformSpecificSetup:(WKWebView*)webView webViewDelegate:(id<WKNavigationDelegate>)webViewDelegate handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle{
- (void) _setupInstance:(WKWebView*)webView webViewDelegate:(id<WKNavigationDelegate>)webViewDelegate handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle{
_webView = webView;
_webViewDelegate = webViewDelegate;
_webView.navigationDelegate = self;
Expand Down Expand Up @@ -165,7 +165,6 @@ - (void)webView:(WKWebView *)webView

- (NSString*) _evaluateJavascript:(NSString*)javascriptCommand
{
NSLog(@"----- EVALUATING");
[_webView evaluateJavaScript:javascriptCommand completionHandler:nil];
return NULL;
}
Expand Down
Loading

0 comments on commit 7382589

Please sign in to comment.