explaingit

marcuswestin/webviewjavascriptbridge

14,320Objective-CAudience · developerComplexity · 3/5Setup · moderate

TLDR

Small iOS and macOS library that lets native Objective-C code and in-app web page JavaScript call each other by registering named message handlers.

Mindmap

mindmap
  root((JS Bridge))
    What it does
      Bi-directional messaging
      Named handlers
    Platforms
      iOS UIWebView
      WKWebView
      macOS WebView
    Setup
      CocoaPods
      Manual drag-in
    Use Cases
      Hybrid apps
      Native feature calls
      Web-native bridge
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Send data from native Objective-C code to JavaScript running in a WKWebView and receive a callback response.

USE CASE 2

Call native iOS features from a web page loaded inside an app without using a full hybrid framework.

USE CASE 3

Build a hybrid iOS app where a web view and the native layer exchange messages bi-directionally.

USE CASE 4

Trigger native Objective-C code from a JavaScript button click inside a WKWebView.

Tech stack

Objective-CCocoaPods

Getting it running

Difficulty · moderate Time to first run · 30min

Requires Automatic Reference Counting enabled in the Xcode project, JavaScript setup snippet must be added to every page.

In plain English

This is a small library for Apple's iOS and macOS apps that lets two parts of an app talk to each other. Many apps show web pages inside a panel called a web view. The native side of the app is written in Objective-C, while the page inside the web view runs JavaScript. Normally these two sides cannot easily call each other. This library acts as a bridge so messages can be sent in both directions. It supports the older UIWebView and WebView components as well as the newer WKWebView. The README lists companies and apps that have used it, including Facebook Messenger and Facebook Paper, presented as a small and incomplete sample. You add the library either through CocoaPods, a popular dependency manager for Apple projects, or by dragging its folder into your project by hand. The core idea is handlers. On the Objective-C side you register a named handler, for example one called ObjC Echo, and provide a block of code to run when it is called. The JavaScript side can then call that handler by name and optionally receive a response. The same works in reverse: JavaScript registers named handlers, and the Objective-C code calls them. Each call can carry data and an optional response callback, so one side can send a value and get an answer back. To set this up, the page must paste in a short setup function that the README provides. The documentation also covers a migration note for upgrading between major versions, a reminder that the library depends on Automatic Reference Counting, and a reference list of the methods available on both sides. There is also an unsafe option to speed up message passing by skipping a timing safety check, which the README warns against using if your JavaScript shows alert or confirm popups.

Copy-paste prompts

Prompt 1
Using WebViewJavascriptBridge in an iOS app, register an Objective-C handler called getUserData that returns user profile JSON to the JavaScript side.
Prompt 2
Show me how to call a JavaScript function from Objective-C using WebViewJavascriptBridge and handle the callback response.
Prompt 3
Set up WebViewJavascriptBridge in a WKWebView-based iOS app so the web page can trigger native Objective-C code on button click.
Prompt 4
How do I install WebViewJavascriptBridge via CocoaPods and add the required JavaScript setup snippet to my HTML page?
Open on GitHub → Explain another repo

← marcuswestin on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.