forked from marcuswestin/WebViewJavascriptBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleAppDelegate.m
More file actions
65 lines (50 loc) · 2.99 KB
/
ExampleAppDelegate.m
File metadata and controls
65 lines (50 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#import "ExampleAppDelegate.h"
@implementation ExampleAppDelegate
@synthesize window = _window;
@synthesize javascriptBridge = _javascriptBridge;
- (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];
self.javascriptBridge = [WebViewJavascriptBridge javascriptBridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message from Javascript" message:data delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}];
[self.javascriptBridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"testObjcCallback called: %@", data);
responseCallback(@"Response from testObjcCallback");
}];
[self.javascriptBridge send:@"A string sent from ObjC before Webview has loaded."];
[self.javascriptBridge callHandler:@"testJavascriptHandler" data:[NSDictionary dictionaryWithObject:@"before ready" forKey:@"foo"]];
[self renderButtons:webView];
[self loadExamplePage:webView];
[self.javascriptBridge send:@"A string sent from ObjC after Webview has loaded."];
[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, 400, 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, 400, 130, 45);
}
- (void)sendMessage:(id)sender {
[self.javascriptBridge send:@"A string sent from ObjC to JS"];
}
- (void)callHandler:(id)sender {
[self.javascriptBridge callHandler:@"testJavascriptHandler" data:[NSDictionary dictionaryWithObject:@"Hi there, JS!" forKey:@"greetingFromObjC"] responseCallback:^(id responseData) {
NSLog(@"testJavascriptHandler responded: %@", responseData);
}];
}
- (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