Skip to content

Commit f179fcf

Browse files
committed
improve example app styling and example functionality
1 parent 942c507 commit f179fcf

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

ExampleApp/ExampleApp.html

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
<!doctype html>
22
<html><head>
3-
<style type='text/css'>h1 { color:red; }</style>
3+
<style type='text/css'>
4+
h1 { color:red; }
5+
.logLine { border-bottom:1px solid #ccc; padding:4px 2px; font-family:courier; font-size:11px; }
6+
</style>
47
</head><body>
58
<h1>Javascript Bridge Demo</h1>
69
<script>
710
document.addEventListener('WebViewJavascriptBridgeReady', onBridgeReady, false)
811
function onBridgeReady() {
912

13+
var uniqueId = 1
1014
function log(message, data) {
1115
var log = document.getElementById('log')
1216
var el = document.createElement('div')
13-
el.innerHTML = message + ': ' + JSON.stringify(data)
17+
el.className = 'logLine'
18+
el.innerHTML = uniqueId++ + '. ' + message + (data ? ': ' + JSON.stringify(data) : '')
1419
if (log.children.length) { log.insertBefore(el, log.children[0]) }
1520
else { log.appendChild(el) }
1621
}
@@ -19,23 +24,26 @@ <h1>Javascript Bridge Demo</h1>
1924
})
2025

2126
WebViewJavascriptBridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {
22-
log('testJavascriptHandler', data)
23-
responseCallback({ 'foo':'Right back atcha!' })
27+
log('JS handler testJavascriptHandler was called', data)
28+
responseCallback({ 'Javascript Says':'Right back atcha!' })
2429
})
2530

2631
var button = document.getElementById('buttons').appendChild(document.createElement('button'))
2732
button.innerHTML = 'Send message to ObjC'
28-
button.ontouchstart = function() {
33+
button.ontouchstart = function(e) {
34+
e.preventDefault()
2935
WebViewJavascriptBridge.send('Hello from JS button')
3036
}
3137

3238
document.body.appendChild(document.createElement('br'))
3339

3440
var callbackButton = document.getElementById('buttons').appendChild(document.createElement('button'))
3541
callbackButton.innerHTML = 'Fire testObjcCallback'
36-
callbackButton.ontouchstart = function() {
42+
callbackButton.ontouchstart = function(e) {
43+
e.preventDefault()
44+
log("Calling handler testObjcCallback")
3745
WebViewJavascriptBridge.fireHandler('testObjcCallback', {'foo': 'bar'}, function(response) {
38-
log('Got response:', response)
46+
log('Got response from testObjcCallback:', response)
3947
})
4048
}
4149
}

ExampleApp/ExampleAppDelegate.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
1111
[self.window addSubview:webView];
1212

1313
self.javascriptBridge = [WebViewJavascriptBridge javascriptBridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) {
14-
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message from Javascript" message:data delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
14+
NSLog(@"ObjC received message from JS: %@", data);
15+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ObjC got message from Javascript:" message:data delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
1516
[alert show];
1617
}];
1718

0 commit comments

Comments
 (0)