Latest 0.1.8
License MIT
Platforms ios 8.0
Frameworks UIKit, MapKit

An iOS/OSX bridge for sending messages between Obj-C and JavaScript in WKWebViews, UIWebViews & WebViews.

Who uses WebViewJavascriptBridge?

WebViewJavascriptBridge is used by a range of companies and projects. This is a small and incomplete sample list:

Installation (iOS & OSX)

Installation with CocoaPods

Add this to your podfile and run pod install to install:

`pod 'WebViewJavascriptBridge', '~> 5.0'`

Manual installation

Drag the WebViewJavascriptBridge folder into your project.

In the dialog that appears, uncheck "Copy items into destination group’s folder" and select "Create groups for any folders".


See the Example Apps/ folder. Open either the iOS or OSX project and hit run to see it in action.

To use a WebViewJavascriptBridge in your own project:


1) Import the header file and declare an ivar property:

#import "WebViewJavascriptBridge.h"

@property WebViewJavascriptBridge* bridge;

2) Instantiate WebViewJavascriptBridge with a WKWebView, UIWebView (iOS) or WebView (OSX):

self.bridge = [WebViewJavascriptBridge bridgeForWebView:webView];

3) Register a handler in ObjC, and call a JS handler:

[self.bridge registerHandler:@"ObjC Echo" handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"ObjC Echo called with: %@", data);
[self.bridge callHandler:@"JS Echo" data:nil responseCallback:^(id responseData) {
NSLog(@"ObjC received response: %@", responseData);

4) Copy and paste setupWebViewJavascriptBridge into your JS:

function setupWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) { return callback(WebViewJavascriptBridge); }
if (window.WVJBCallbacks) { return window.WVJBCallbacks.push(callback); }
window.WVJBCallbacks = [callback];
var WVJBIframe = document.createElement('iframe'); = 'none';
WVJBIframe.src = 'https://__bridge_loaded__';
setTimeout(function() { document.documentElement.removeChild(WVJBIframe) }, 0)

5) Finally, call setupWebViewJavascriptBridge and then use the bridge to register handlers and call ObjC handlers:

setupWebViewJavascriptBridge(function(bridge) {

/* Initialize your app here */

bridge.registerHandler('JS Echo', function(data, responseCallback) {
console.log("JS Echo called with:", data)
bridge.callHandler('ObjC Echo', {'key':'value'}, function responseCallback(responseData) {
console.log("JS received response:", responseData)


[WebViewJavascriptBridge bridgeForWebView:(WKWebVIew/UIWebView/WebView*)webview

Create a javascript bridge for the given web view.


[WebViewJavascriptBridge bridgeForWebView:webView];
[bridge registerHandler:(NSString*)handlerName handler:(WVJBHandler)handler]

Register a handler called handlerName. The javascript can then call this handler with WebViewJavascriptBridge.callHandler("handlerName").


[self.bridge registerHandler:@"getScreenHeight" handler:^(id data, WVJBResponseCallback responseCallback) {
responseCallback([NSNumber numberWithInt:[UIScreen mainScreen].bounds.size.height]);
[self.bridge registerHandler:@"log" handler:^(id data, WVJBResponseCallback responseCallback) {
NSLog(@"Log: %@", data);
[bridge callHandler:(NSString*)handlerName data:(id)data]
[bridge callHandler:(NSString*)handlerName data:(id)data responseCallback:(WVJBResponseCallback)callback]

Call the javascript handler called handlerName. If a responseCallback block is given the javascript handler can respond.


[self.bridge callHandler:@"showAlert" data:@"Hi from ObjC to JS!"];
[self.bridge callHandler:@"getCurrentPageUrl" data:nil responseCallback:^(id responseData) {
NSLog(@"Current UIWebView page URL is: %@", responseData);

[bridge setWebViewDelegate:(id)webViewDelegate]

Optionally, set a WKNavigationDelegate/UIWebViewDelegate if you need to respond to the web view’s lifecycle events.

[bridge disableJavscriptAlertBoxSafetyTimeout]

UNSAFE. Speed up bridge message passing by disabling the setTimeout safety check. It is only safe to disable this safety check if you do not call any of the javascript popup box functions (alert, confirm, and prompt). If you call any of these functions from the bridged javascript code, the app will hang.


[self.bridge disableJavscriptAlertBoxSafetyTimeout];

Javascript API

bridge.registerHandler("handlerName", function(responseData) { ... })

Register a handler called handlerName. The ObjC can then call this handler with [bridge callHandler:"handlerName" data:@"Foo"] and [bridge callHandler:"handlerName" data:@"Foo" responseCallback:^(id responseData) { ... }]


bridge.registerHandler("showAlert", function(data) { alert(data) })
bridge.registerHandler("getCurrentPageUrl", function(data, responseCallback) {
bridge.callHandler("handlerName", data)
bridge.callHandler("handlerName", data, function responseCallback(responseData) { ... })

Call an ObjC handler called handlerName. If a responseCallback function is given the ObjC handler can respond.


bridge.callHandler("Log", "Foo")
bridge.callHandler("getScreenHeight", null, function(response) {
alert('Screen height:' + response)

Calling bridge.disableJavscriptAlertBoxSafetyTimeout() has the same effect as calling [bridge disableJavscriptAlertBoxSafetyTimeout]; in ObjC.




To run the example project, clone the repo, and run pod install from the Example directory first.



WJwebviewjavascriptbridge is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod "WJwebviewjavascriptbridge"


wujiegoodluck, [email protected]


WJwebviewjavascriptbridge is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "WJwebviewjavascriptbridge",
    "version": "0.1.8",
    "summary": "OCu4e0eJSu7684u4ea4u4e92",
    "description": "+OCu4e0eJSu7684u4ea4u4e92",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "wujiegoodluck": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.1.8"
    "platforms": {
        "ios": "8.0"
    "source_files": "WJwebviewjavascriptbridge/Classes/**/*",
    "resource_bundles": {
        "WJwebviewjavascriptbridge": [
    "public_header_files": "WJwebviewjavascriptbridge/Classes/**/*.h",
    "frameworks": [

Pin It on Pinterest

Share This