Skip to content

Commit

Permalink
Update JS initialization snippet. Fix marcuswestin#56 (Thanks @refrac…
Browse files Browse the repository at this point in the history
…talize!). Possibly fixes marcuswestin#52.
  • Loading branch information
marcuswestin committed Oct 26, 2013
1 parent fb46cc0 commit 4d4fc97
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Example Apps/ExampleApp-OSX/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ - (void)_sendMessage {
}

- (void)_callHandler {
is data = @{ @"greetingFromObjC": @"Hi there, JS!" };
id data = @{ @"greetingFromObjC": @"Hi there, JS!" };
[_bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
NSLog(@"testJavascriptHandler responded: %@", response);
}];
Expand Down
17 changes: 13 additions & 4 deletions Example Apps/ExampleApp.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ <h1>WebViewJavascriptBridge Demo</h1>
window.onerror = function(err) {
log('window.onerror: ' + err)
}
document.addEventListener('WebViewJavascriptBridgeReady', onBridgeReady, false)
function onBridgeReady(event) {
var bridge = event.bridge

function connectWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
} else {
document.addEventListener('WebViewJavascriptBridgeReady', function() {
callback(WebViewJavascriptBridge)
}, false)
}
}

connectWebViewJavascriptBridge(function(bridge) {
var uniqueId = 1
function log(message, data) {
var log = document.getElementById('log')
Expand Down Expand Up @@ -60,7 +69,7 @@ <h1>WebViewJavascriptBridge Demo</h1>
log('JS got response', response)
})
}
}
})
</script>
<div id='buttons'></div> <div id='log'></div>
</body></html>
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@ To use a WebViewJavascriptBridge in your own project:

4) Finally, set up the javascript side:

document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady(event) {
var bridge = event.bridge
function connectWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
} else {
document.addEventListener('WebViewJavascriptBridgeReady', function() {
callback(WebViewJavascriptBridge)
}, false)
}
}

connectWebViewJavascriptBridge(function(bridge) {
/* Init your app here */

bridge.init(function(message, responseCallback) {
alert('Received message: ' + message)
if (responseCallback) {
Expand All @@ -62,7 +74,7 @@ To use a WebViewJavascriptBridge in your own project:
bridge.send('Please respond to this', function responseCallback(responseData) {
console.log("Javascript got its response", responseData)
})
}, false)
})

Contributors & Forks
--------------------
Expand Down

0 comments on commit 4d4fc97

Please sign in to comment.