Skip to content

Commit

Permalink
Updated the example app to use WKWebView
Browse files Browse the repository at this point in the history
  • Loading branch information
lokimeyburg committed Oct 15, 2014
1 parent f39324d commit b535dc2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
4 changes: 4 additions & 0 deletions Example Apps/ExampleApp-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
0E8082DB19EDC32300479452 /* WKWebViewJavascriptBridge.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E8082DA19EDC32300479452 /* WKWebViewJavascriptBridge.m */; };
0E8082DD19EDD98700479452 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0E8082DC19EDD98700479452 /* WebKit.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
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 */; };
Expand All @@ -24,6 +25,7 @@
/* Begin PBXFileReference section */
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; };
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>"; };
Expand All @@ -48,6 +50,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
0E8082DD19EDD98700479452 /* WebKit.framework in Frameworks */,
2CEB3EC01602563600548120 /* UIKit.framework in Frameworks */,
2CEB3EC21602563600548120 /* Foundation.framework in Frameworks */,
2CEB3EC41602563600548120 /* CoreGraphics.framework in Frameworks */,
Expand Down Expand Up @@ -116,6 +119,7 @@
2CEB3EBE1602563600548120 /* Frameworks */ = {
isa = PBXGroup;
children = (
0E8082DC19EDD98700479452 /* WebKit.framework */,
2CEB3EBF1602563600548120 /* UIKit.framework */,
2CEB3EC11602563600548120 /* Foundation.framework */,
2CEB3EC31602563600548120 /* CoreGraphics.framework */,
Expand Down
17 changes: 16 additions & 1 deletion Example Apps/ExampleApp-iOS/ExampleAppViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@

#import <UIKit/UIKit.h>

@interface ExampleAppViewController : UINavigationController <UIWebViewDelegate>

#if defined(__IPHONE_8_0)
#import <WebKit/WebKit.h>
#define EXAMPLE_WEBVIEW_TYPE WKWebView
#define EXAMPLE_WEBVIEW_DELEGATE_TYPE NSObject<WKNavigationDelegate>
#define EXAMPLE_WEBVIEW_DELEGATE_CONTROLLER UINavigationController<WKNavigationDelegate>
#define EXAMPLE_BRIDGE_TYPE WKWebViewJavascriptBridge
#else
#define EXAMPLE_WEBVIEW_TYPE UIWebView
#define EXAMPLE_WEBVIEW_DELEGATE_TYPE NSObject<UIWebViewDelegate>
#define EXAMPLE_WEBVIEW_DELEGATE_CONTROLLER UINavigationController<UIWebViewDelegate>
#define EXAMPLE_BRIDGE_TYPE WebViewJavascriptBridge
#endif


@interface ExampleAppViewController : EXAMPLE_WEBVIEW_DELEGATE_CONTROLLER

@end
40 changes: 29 additions & 11 deletions Example Apps/ExampleApp-iOS/ExampleAppViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,44 @@
//

#import "ExampleAppViewController.h"

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

@interface ExampleAppViewController ()
@property WebViewJavascriptBridge* bridge;

@property EXAMPLE_BRIDGE_TYPE* bridge;

@end

@implementation ExampleAppViewController

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

UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:webView];
#if defined(__IPHONE_8_0)
WKWebView* webView = [[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

[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);
Expand All @@ -53,7 +71,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"webViewDidFinishLoad");
}

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

UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Expand Down Expand Up @@ -92,7 +110,7 @@ - (void)callHandler:(id)sender {
}];
}

- (void)loadExamplePage:(UIWebView*)webView {
- (void)loadExamplePage:(EXAMPLE_WEBVIEW_TYPE*)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
1 change: 1 addition & 0 deletions Example Apps/ExampleApp.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!doctype html>
<html><head>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<style type='text/css'>
html { font-family:Helvetica; color:#222; }
h1 { color:steelblue; font-size:24px; margin-top:24px; }
Expand Down
9 changes: 1 addition & 8 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
#define kCustomProtocolScheme @"wvjbscheme"
#define kQueueHasMessage @"__WVJB_QUEUE_MESSAGE__"


#if defined(__IPHONE_8_0)
#import <WebKit/WebKit.h>
#define WVJB_PLATFORM_IOS
// #define WVJB_WEBVIEW_TYPE WKWebView
// #define WVJB_WEBVIEW_DELEGATE_TYPE NSObject<WKNavigationDelegate>
#endif

#import <WebKit/WebKit.h>
typedef void (^WVJBResponseCallback)(id responseData);
typedef void (^WVJBHandler)(id data, WVJBResponseCallback responseCallback);

Expand Down
2 changes: 2 additions & 0 deletions WebViewJavascriptBridge/WKWebViewJavascriptBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ - (void)webView:(WKWebView *)webView

if (strongDelegate && [strongDelegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
[_webViewDelegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}

Expand Down

0 comments on commit b535dc2

Please sign in to comment.