DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. How to Communicate Between WebView and Native Client
XLinkedInReddit
MediumFrontend Engineering

How to Communicate Between WebView and Native Client

D
DevPrep Team
February 9, 2026·2 min read·0
Table of Contents
  • Why This Matters
  • JavaScript Bridge (Android)
  • WKScriptMessageHandler (iOS)
  • URL Scheme Interception
  • postMessage Pattern
  • Production Best Practices
  • Security Concerns
  • Summary

By Rahul — Google Frontend Engineer

Why This Matters

Many apps embed web content inside native apps using WebView. The web page needs to talk to the native app (access camera, GPS, file system) and the native app needs to send data to the web page. Getting this communication right is critical for hybrid apps.

JavaScript Bridge (Android)

Android's WebView lets you inject a JavaScript interface:

// Android (Kotlin) — expose native methods to JS
class NativeBridge {
    @JavascriptInterface
    fun showToast(message: String) {
        Toast.makeText(context, message, Toast.LENGTH_SHORT).show()
    }
    
    @JavascriptInterface
    fun getDeviceId(): String {
        return Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
    }
}
webView.addJavascriptInterface(NativeBridge(), "Android")

// JavaScript — call native methods
window.Android.showToast("Hello from web!");
const deviceId = window.Android.getDeviceId();

WKScriptMessageHandler (iOS)

// Swift — receive messages from JS
class MessageHandler: NSObject, WKScriptMessageHandler {
    func userContentController(_ controller: WKUserContentController,
                               didReceive message: WKScriptMessage) {
        if message.name == "nativeAction" {
            let body = message.body as? [String: Any]
            handleAction(body)
        }
    }
}

// JavaScript — send to native
window.webkit.messageHandlers.nativeAction.postMessage({
    action: "share",
    data: { url: "https://example.com", title: "Check this out" }
});

URL Scheme Interception

The oldest and most universal method. Web page navigates to a custom URL, native app intercepts it:

// JavaScript
window.location.href = "myapp://action?param=value";

// Or using an iframe (non-disruptive)
const iframe = document.createElement("iframe");
iframe.src = "myapp://getLocation";
document.body.appendChild(iframe);
setTimeout(() => iframe.remove(), 100);

postMessage Pattern

// Web → Native: post a message
window.postMessage(JSON.stringify({
    type: "REQUEST_LOCATION",
    requestId: "abc123"
}), "*");

// Native → Web: evaluate JavaScript
webView.evaluateJavaScript(
    "window.dispatchEvent(new CustomEvent('nativeResponse', { detail: { lat: 37.7, lng: -122.4, requestId: 'abc123' } }))"
);

// Web listens for native responses
window.addEventListener("nativeResponse", (e) => {
    console.log(e.detail); // { lat: 37.7, lng: -122.4 }
});

Production Best Practices

  • Always check if the bridge exists: if (window.Android) before calling
  • Use a message queue for async responses — match request/response with IDs
  • Validate all data coming from the bridge — never trust it blindly
  • Version your bridge API — old app versions may not have new methods
  • Handle the case where the page runs outside WebView (regular browser)

Security Concerns

  • JavaScript bridges expose native functionality to web content — restrict which URLs can use the bridge
  • Never expose sensitive methods (file access, contacts) without user confirmation
  • On Android, @JavascriptInterface methods are accessible to any JS on the page, including injected scripts

Summary

WebView-native communication uses JavaScript bridges (Android), message handlers (iOS), URL scheme interception, or postMessage. Always version your bridge API, validate data, and handle the non-WebView case gracefully.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • Why This Matters
  • JavaScript Bridge (Android)
  • WKScriptMessageHandler (iOS)
  • URL Scheme Interception
  • postMessage Pattern
  • Production Best Practices
  • Security Concerns
  • Summary

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.