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.
You can toggle between UIWebView and WKWebView in the example app
- Loading branch information
1 parent
589dc2a
commit 7382589
Showing
9 changed files
with
184 additions
and
81 deletions.
There are no files selected for viewing
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
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
This file was deleted.
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,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
101
Example Apps/ExampleApp-iOS/ExampleUIWebViewController.m
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,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 |
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,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 |
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
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
Oops, something went wrong.