Skip to content

Commit 7382589

Browse files
committed
You can toggle between UIWebView and WKWebView in the example app
1 parent 589dc2a commit 7382589

File tree

9 files changed

+184
-81
lines changed

9 files changed

+184
-81
lines changed

Example Apps/ExampleApp-iOS.xcodeproj/project.pbxproj

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
/* Begin PBXBuildFile section */
1010
0E50601C1A01B442000BEEEA /* WebViewJavascriptBridgeBase.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E50601B1A01B442000BEEEA /* WebViewJavascriptBridgeBase.m */; };
1111
0E8082DB19EDC32300479452 /* WKWebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E8082DA19EDC32300479452 /* WKWebViewJavascriptBridge.m */; };
12+
0ECB01441A0EE1F20037FF4E /* ExampleWKWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0ECB01431A0EE1F20037FF4E /* ExampleWKWebViewController.m */; };
1213
2C1562B5176B9F8400B4AE50 /* WebViewJavascriptBridge.js.txt in Resources */ = {isa = PBXBuildFile; fileRef = 2C1562B4176B9F8400B4AE50 /* WebViewJavascriptBridge.js.txt */; };
1314
2C1562C0176BA63500B4AE50 /* WebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C1562A9176B9F6200B4AE50 /* WebViewJavascriptBridge.m */; };
14-
2C45CA2C1884AD520002A4E2 /* ExampleAppViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C45CA2B1884AD520002A4E2 /* ExampleAppViewController.m */; };
15+
2C45CA2C1884AD520002A4E2 /* ExampleUIWebViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C45CA2B1884AD520002A4E2 /* ExampleUIWebViewController.m */; };
1516
2CA045BF17117439006DEE8B /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 2CA045B717117439006DEE8B /* InfoPlist.strings */; };
1617
2CA045C217117439006DEE8B /* ExampleAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2CA045BD17117439006DEE8B /* ExampleAppDelegate.m */; };
1718
2CA045C317117439006DEE8B /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2CA045BE17117439006DEE8B /* main.m */; };
@@ -28,11 +29,13 @@
2829
0E8082D919EDC32300479452 /* WKWebViewJavascriptBridge.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKWebViewJavascriptBridge.h; sourceTree = "<group>"; };
2930
0E8082DA19EDC32300479452 /* WKWebViewJavascriptBridge.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WKWebViewJavascriptBridge.m; sourceTree = "<group>"; };
3031
0E8082DC19EDD98700479452 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
32+
0ECB01421A0EE1BA0037FF4E /* ExampleWKWebViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ExampleWKWebViewController.h; sourceTree = "<group>"; };
33+
0ECB01431A0EE1F20037FF4E /* ExampleWKWebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleWKWebViewController.m; sourceTree = "<group>"; };
3134
2C1562A8176B9F6200B4AE50 /* WebViewJavascriptBridge.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebViewJavascriptBridge.h; sourceTree = "<group>"; };
3235
2C1562A9176B9F6200B4AE50 /* WebViewJavascriptBridge.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = WebViewJavascriptBridge.m; sourceTree = "<group>"; };
3336
2C1562B4176B9F8400B4AE50 /* WebViewJavascriptBridge.js.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = WebViewJavascriptBridge.js.txt; sourceTree = "<group>"; };
34-
2C45CA2A1884AD520002A4E2 /* ExampleAppViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExampleAppViewController.h; sourceTree = "<group>"; };
35-
2C45CA2B1884AD520002A4E2 /* ExampleAppViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleAppViewController.m; sourceTree = "<group>"; };
37+
2C45CA2A1884AD520002A4E2 /* ExampleUIWebViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExampleUIWebViewController.h; sourceTree = "<group>"; };
38+
2C45CA2B1884AD520002A4E2 /* ExampleUIWebViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleUIWebViewController.m; sourceTree = "<group>"; };
3639
2CA045B817117439006DEE8B /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
3740
2CA045B917117439006DEE8B /* ExampleApp-iOS-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "ExampleApp-iOS-Info.plist"; sourceTree = "<group>"; };
3841
2CA045BA17117439006DEE8B /* ExampleApp-iOS-Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ExampleApp-iOS-Prefix.pch"; sourceTree = "<group>"; };
@@ -82,8 +85,10 @@
8285
2CA0465B1711AC8D006DEE8B /* ExampleApp.html */,
8386
2CA045BC17117439006DEE8B /* ExampleAppDelegate.h */,
8487
2CA045BD17117439006DEE8B /* ExampleAppDelegate.m */,
85-
2C45CA2A1884AD520002A4E2 /* ExampleAppViewController.h */,
86-
2C45CA2B1884AD520002A4E2 /* ExampleAppViewController.m */,
88+
2C45CA2A1884AD520002A4E2 /* ExampleUIWebViewController.h */,
89+
2C45CA2B1884AD520002A4E2 /* ExampleUIWebViewController.m */,
90+
0ECB01421A0EE1BA0037FF4E /* ExampleWKWebViewController.h */,
91+
0ECB01431A0EE1F20037FF4E /* ExampleWKWebViewController.m */,
8792
2C1562A7176B9F5400B4AE50 /* WebViewJavascriptBridge */,
8893
2CA046211711A94E006DEE8B /* Supporting Files */,
8994
);
@@ -197,7 +202,8 @@
197202
files = (
198203
2C1562C0176BA63500B4AE50 /* WebViewJavascriptBridge.m in Sources */,
199204
0E8082DB19EDC32300479452 /* WKWebViewJavascriptBridge.m in Sources */,
200-
2C45CA2C1884AD520002A4E2 /* ExampleAppViewController.m in Sources */,
205+
2C45CA2C1884AD520002A4E2 /* ExampleUIWebViewController.m in Sources */,
206+
0ECB01441A0EE1F20037FF4E /* ExampleWKWebViewController.m in Sources */,
201207
2CA045C217117439006DEE8B /* ExampleAppDelegate.m in Sources */,
202208
0E50601C1A01B442000BEEEA /* WebViewJavascriptBridgeBase.m in Sources */,
203209
2CA045C317117439006DEE8B /* main.m in Sources */,

Example Apps/ExampleApp-iOS/ExampleAppDelegate.m

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
#import "ExampleAppDelegate.h"
2-
#import "ExampleAppViewController.h"
2+
#import "ExampleUIWebViewController.h"
3+
#import "ExampleWKWebViewController.h"
34

45
@implementation ExampleAppDelegate
56

67
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
8+
9+
ExampleUIWebViewController* UIWebViewExampleController = [[ExampleUIWebViewController alloc] init];
10+
UIWebViewExampleController.tabBarItem.title = @"UIWebView";
11+
12+
ExampleWKWebViewController* WKWebViewExampleController = [[ExampleWKWebViewController alloc] init];
13+
WKWebViewExampleController.tabBarItem.title = @"WKWebView";
14+
15+
UITabBarController *tabBarController = [[UITabBarController alloc] init];
16+
[tabBarController addChildViewController:UIWebViewExampleController];
17+
[tabBarController addChildViewController:WKWebViewExampleController];
18+
19+
20+
721
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
8-
self.window.rootViewController = [ExampleAppViewController new];
22+
self.window.rootViewController = tabBarController;
923
[self.window makeKeyAndVisible];
1024
return YES;
1125
}

Example Apps/ExampleApp-iOS/ExampleAppViewController.h

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// ExampleAppViewController.h
3+
// ExampleApp-iOS
4+
//
5+
// Created by Marcus Westin on 1/13/14.
6+
// Copyright (c) 2014 Marcus Westin. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface ExampleUIWebViewController : UINavigationController <UIWebViewDelegate>
12+
13+
@end
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//
2+
// ExampleAppViewController.m
3+
// ExampleApp-iOS
4+
//
5+
// Created by Marcus Westin on 1/13/14.
6+
// Copyright (c) 2014 Marcus Westin. All rights reserved.
7+
//
8+
9+
#import "ExampleUIWebViewController.h"
10+
#import "WebViewJavascriptBridge.h"
11+
12+
@interface ExampleUIWebViewController ()
13+
@property WebViewJavascriptBridge* bridge;
14+
@end
15+
16+
@implementation ExampleUIWebViewController
17+
18+
- (void)viewWillAppear:(BOOL)animated {
19+
if (_bridge) { return; }
20+
21+
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
22+
[self.view addSubview:webView];
23+
24+
[WebViewJavascriptBridge enableLogging];
25+
26+
_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
27+
NSLog(@"ObjC received message from JS: %@", data);
28+
responseCallback(@"Response for message from ObjC");
29+
}];
30+
31+
[_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
32+
NSLog(@"testObjcCallback called: %@", data);
33+
responseCallback(@"Response from testObjcCallback");
34+
}];
35+
36+
[_bridge send:@"A string sent from ObjC before Webview has loaded." responseCallback:^(id responseData) {
37+
NSLog(@"objc got response! %@", responseData);
38+
}];
39+
40+
[_bridge callHandler:@"testJavascriptHandler" data:@{ @"foo":@"before ready" }];
41+
42+
[self renderButtons:webView];
43+
[self loadExamplePage:webView];
44+
45+
[_bridge send:@"A string sent from ObjC after Webview has loaded."];
46+
}
47+
48+
- (void)webViewDidStartLoad:(UIWebView *)webView {
49+
NSLog(@"webViewDidStartLoad");
50+
}
51+
52+
- (void)webViewDidFinishLoad:(UIWebView *)webView {
53+
NSLog(@"webViewDidFinishLoad");
54+
}
55+
56+
- (void)renderButtons:(UIWebView*)webView {
57+
UIFont* font = [UIFont fontWithName:@"HelveticaNeue" size:12.0];
58+
59+
UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
60+
[messageButton setTitle:@"Send message" forState:UIControlStateNormal];
61+
[messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
62+
[self.view insertSubview:messageButton aboveSubview:webView];
63+
messageButton.frame = CGRectMake(10, 414, 100, 35);
64+
messageButton.titleLabel.font = font;
65+
messageButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.75];
66+
67+
UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
68+
[callbackButton setTitle:@"Call handler" forState:UIControlStateNormal];
69+
[callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside];
70+
[self.view insertSubview:callbackButton aboveSubview:webView];
71+
callbackButton.frame = CGRectMake(110, 414, 100, 35);
72+
callbackButton.titleLabel.font = font;
73+
74+
UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
75+
[reloadButton setTitle:@"Reload webview" forState:UIControlStateNormal];
76+
[reloadButton addTarget:webView action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
77+
[self.view insertSubview:reloadButton aboveSubview:webView];
78+
reloadButton.frame = CGRectMake(210, 414, 100, 35);
79+
reloadButton.titleLabel.font = font;
80+
}
81+
82+
- (void)sendMessage:(id)sender {
83+
[_bridge send:@"A string sent from ObjC to JS" responseCallback:^(id response) {
84+
NSLog(@"sendMessage got response: %@", response);
85+
}];
86+
}
87+
88+
- (void)callHandler:(id)sender {
89+
id data = @{ @"greetingFromObjC": @"Hi there, JS!" };
90+
[_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
91+
NSLog(@"testJavascriptHandler responded: %@", response);
92+
}];
93+
}
94+
95+
- (void)loadExamplePage:(UIWebView*)webView {
96+
NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
97+
NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
98+
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
99+
[webView loadHTMLString:appHtml baseURL:baseURL];
100+
}
101+
@end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// ExampleAppViewController.h
3+
// ExampleApp-iOS
4+
//
5+
// Created by Marcus Westin on 1/13/14.
6+
// Copyright (c) 2014 Marcus Westin. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
#import <WebKit/WebKit.h>
11+
12+
@interface ExampleWKWebViewController : UINavigationController<WKNavigationDelegate>
13+
14+
@end

Example Apps/ExampleApp-iOS/ExampleAppViewController.m renamed to Example Apps/ExampleApp-iOS/ExampleWKWebViewController.m

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,30 @@
66
// Copyright (c) 2014 Marcus Westin. All rights reserved.
77
//
88

9-
#import "ExampleAppViewController.h"
10-
11-
#if defined(exampleSupportsWKWebKit)
9+
#import "ExampleWKWebViewController.h"
1210
#import "WKWebViewJavascriptBridge.h"
13-
# else
14-
#import "WebViewJavascriptBridge.h"
15-
#endif
1611

17-
@interface ExampleAppViewController ()
12+
@interface ExampleWKWebViewController ()
1813

19-
@property EXAMPLE_BRIDGE_TYPE* bridge;
14+
@property WKWebViewJavascriptBridge* bridge;
2015

2116
@end
2217

23-
@implementation ExampleAppViewController
18+
@implementation ExampleWKWebViewController
2419

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

28-
#if defined(exampleSupportsWKWebKit)
29-
WKWebView* webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:self.view.bounds];
30-
webView.navigationDelegate = self;
31-
[self.view addSubview:webView];
32-
[WKWebViewJavascriptBridge enableLogging];
33-
_bridge = [WKWebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
34-
NSLog(@"ObjC received message from JS: %@", data);
35-
responseCallback(@"Response for message from ObjC");
36-
}];
37-
#else
38-
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
39-
[self.view addSubview:webView];
40-
[WebViewJavascriptBridge enableLogging];
41-
_bridge = [WebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
42-
NSLog(@"ObjC received message from JS: %@", data);
43-
responseCallback(@"Response for message from ObjC");
44-
}];
45-
#endif
23+
WKWebView* webView = [[NSClassFromString(@"WKWebView") alloc] initWithFrame:self.view.bounds];
24+
webView.navigationDelegate = self;
25+
[self.view addSubview:webView];
26+
[WKWebViewJavascriptBridge enableLogging];
27+
_bridge = [WKWebViewJavascriptBridge bridgeForWebView:webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
28+
NSLog(@"ObjC received message from JS: %@", data);
29+
responseCallback(@"Response for message from ObjC");
30+
}];
31+
4632

47-
4833

4934
[_bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
5035
NSLog(@"testObjcCallback called: %@", data);
@@ -71,29 +56,29 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
7156
NSLog(@"webViewDidFinishLoad");
7257
}
7358

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

7762
UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
78-
[messageButton setTitle:@"Send message" forState:UIControlStateNormal];
79-
[messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
80-
[self.view insertSubview:messageButton aboveSubview:webView];
81-
messageButton.frame = CGRectMake(10, 414, 100, 35);
63+
[messageButton setTitle:@"Send message" forState:UIControlStateNormal];
64+
[messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
65+
[self.view insertSubview:messageButton aboveSubview:webView];
66+
messageButton.frame = CGRectMake(10, 414, 100, 35);
8267
messageButton.titleLabel.font = font;
8368
messageButton.backgroundColor = [UIColor colorWithWhite:1 alpha:0.75];
84-
69+
8570
UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
8671
[callbackButton setTitle:@"Call handler" forState:UIControlStateNormal];
8772
[callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside];
8873
[self.view insertSubview:callbackButton aboveSubview:webView];
89-
callbackButton.frame = CGRectMake(110, 414, 100, 35);
74+
callbackButton.frame = CGRectMake(110, 414, 100, 35);
9075
callbackButton.titleLabel.font = font;
9176

9277
UIButton* reloadButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
9378
[reloadButton setTitle:@"Reload webview" forState:UIControlStateNormal];
9479
[reloadButton addTarget:webView action:@selector(reload) forControlEvents:UIControlEventTouchUpInside];
9580
[self.view insertSubview:reloadButton aboveSubview:webView];
96-
reloadButton.frame = CGRectMake(210, 414, 100, 35);
81+
reloadButton.frame = CGRectMake(210, 414, 100, 35);
9782
reloadButton.titleLabel.font = font;
9883
}
9984

@@ -110,7 +95,7 @@ - (void)callHandler:(id)sender {
11095
}];
11196
}
11297

113-
- (void)loadExamplePage:(EXAMPLE_WEBVIEW_TYPE*)webView {
98+
- (void)loadExamplePage:(WKWebView*)webView {
11499
NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"ExampleApp" ofType:@"html"];
115100
NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
116101
NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];

WebViewJavascriptBridge/WKWebViewJavascriptBridge.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ + (instancetype)bridgeForWebView:(WKWebView*)webView webViewDelegate:(NSObject<W
3434
+ (instancetype)bridgeForWebView:(WKWebView*)webView webViewDelegate:(NSObject<WKNavigationDelegate>*)webViewDelegate handler:(WVJBHandler)messageHandler resourceBundle:(NSBundle*)bundle
3535
{
3636
WKWebViewJavascriptBridge* bridge = [[WKWebViewJavascriptBridge alloc] init];
37-
[bridge _platformSpecificSetup:webView webViewDelegate:webViewDelegate handler:messageHandler resourceBundle:bundle];
37+
[bridge _setupInstance:webView webViewDelegate:webViewDelegate handler:messageHandler resourceBundle:bundle];
3838
[bridge reset];
3939
return bridge;
4040
}
@@ -81,7 +81,7 @@ - (void)dealloc {
8181
/* WKWebView Specific Internals
8282
******************************/
8383

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

166166
- (NSString*) _evaluateJavascript:(NSString*)javascriptCommand
167167
{
168-
NSLog(@"----- EVALUATING");
169168
[_webView evaluateJavaScript:javascriptCommand completionHandler:nil];
170169
return NULL;
171170
}

0 commit comments

Comments
 (0)