diff --git a/Example Apps/ExampleApp-OSX/AppDelegate.m b/Example Apps/ExampleApp-OSX/AppDelegate.m index 990f9779..4e77da67 100644 --- a/Example Apps/ExampleApp-OSX/AppDelegate.m +++ b/Example Apps/ExampleApp-OSX/AppDelegate.m @@ -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); }]; diff --git a/Example Apps/ExampleApp.html b/Example Apps/ExampleApp.html index 30efb6c8..bf5b7bc7 100644 --- a/Example Apps/ExampleApp.html +++ b/Example Apps/ExampleApp.html @@ -12,9 +12,18 @@

WebViewJavascriptBridge Demo

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') @@ -60,7 +69,7 @@

WebViewJavascriptBridge Demo

log('JS got response', response) }) } - } + })
diff --git a/README.md b/README.md index f28c52b3..ef6eb762 100644 --- a/README.md +++ b/README.md @@ -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) { @@ -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 --------------------