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.
Have the example iOS app use a UIViewController instead of adding the…
… webview directly to the app window. Fixes marcuswestin#65 on GH
- Loading branch information
1 parent
cd1b1a7
commit 4b95cde
Showing
5 changed files
with
111 additions
and
79 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,5 @@ | ||
#import <UIKit/UIKit.h> | ||
#import "WebViewJavascriptBridge.h" | ||
|
||
@interface ExampleAppDelegate : UIResponder <UIApplicationDelegate, UIWebViewDelegate> | ||
|
||
@property (strong, nonatomic) UIWindow *window; | ||
@property (strong, nonatomic) WebViewJavascriptBridge *javascriptBridge; | ||
|
||
- (void)renderButtons:(UIWebView*)webView; | ||
- (void)loadExamplePage:(UIWebView*)webView; | ||
|
||
@interface ExampleAppDelegate : UIResponder <UIApplicationDelegate> | ||
@property (nonatomic) UIWindow *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 |
---|---|---|
@@ -1,81 +1,13 @@ | ||
#import "ExampleAppDelegate.h" | ||
#import "ExampleAppViewController.h" | ||
|
||
@implementation ExampleAppDelegate | ||
|
||
@synthesize window = _window; | ||
@synthesize javascriptBridge = _bridge; | ||
|
||
- (void)webViewDidStartLoad:(UIWebView *)webView { | ||
NSLog(@"webViewDidStartLoad"); | ||
} | ||
|
||
- (void)webViewDidFinishLoad:(UIWebView *)webView { | ||
NSLog(@"webViewDidFinishLoad"); | ||
} | ||
|
||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { | ||
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; | ||
UIWebView* webView = [[UIWebView alloc] initWithFrame:self.window.bounds]; | ||
[self.window 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."]; | ||
|
||
self.window.rootViewController = [ExampleAppViewController new]; | ||
[self.window makeKeyAndVisible]; | ||
return YES; | ||
} | ||
|
||
- (void)renderButtons:(UIWebView*)webView { | ||
UIButton *messageButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | ||
[messageButton setTitle:@"Send message" forState:UIControlStateNormal]; | ||
[messageButton addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside]; | ||
[self.window insertSubview:messageButton aboveSubview:webView]; | ||
messageButton.frame = CGRectMake(20, 414, 130, 45); | ||
|
||
UIButton *callbackButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; | ||
[callbackButton setTitle:@"Call handler" forState:UIControlStateNormal]; | ||
[callbackButton addTarget:self action:@selector(callHandler:) forControlEvents:UIControlEventTouchUpInside]; | ||
[self.window insertSubview:callbackButton aboveSubview:webView]; | ||
callbackButton.frame = CGRectMake(170, 414, 130, 45); | ||
} | ||
|
||
- (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]; | ||
[webView loadHTMLString:appHtml baseURL:nil]; | ||
} | ||
|
||
@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,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 ExampleAppViewController : UINavigationController <UIWebViewDelegate> | ||
|
||
@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,88 @@ | ||
// | ||
// ExampleAppViewController.m | ||
// ExampleApp-iOS | ||
// | ||
// Created by Marcus Westin on 1/13/14. | ||
// Copyright (c) 2014 Marcus Westin. All rights reserved. | ||
// | ||
|
||
#import "ExampleAppViewController.h" | ||
#import "WebViewJavascriptBridge.h" | ||
|
||
@interface ExampleAppViewController () | ||
@property WebViewJavascriptBridge* bridge; | ||
@end | ||
|
||
@implementation ExampleAppViewController | ||
|
||
- (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 { | ||
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(20, 414, 130, 45); | ||
|
||
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(170, 414, 130, 45); | ||
} | ||
|
||
- (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]; | ||
[webView loadHTMLString:appHtml baseURL:nil]; | ||
} | ||
@end |