How karosli/kkjsbridge works
Project Type and Purpose
• This is a hybrid iOS framework (Objective-C + TypeScript) plus a demo app that bridges WKWebView and native code.
• Purpose:
• Provide a modular JSBridge implementation (message handler, module registry, synchronous/asynchronous calls).
• Hook and proxy AJAX/Fetch requests to enable offline packages, body preservation, sync requests, external proxies.
• Manage cookies between JavaScript (document.cookie) and native NSHTTPCookieStorage.
• Reuse WKWebView instances via a pool.
High-Level Architecture Overview
[Application Layer]
• Demo App (UIKit controllers, UIWebView wrapper)
[WebView Layer]
• KKWebView: subclass of WKWebView with extension categories (KKJSBridgeEngine attachment)
• KKWebViewPool: pre-allocates/reuses KKWebView instances
[Bridge Core]
• KKJSBridgeEngine: instantiates per WebView, holds configuration and module register
• Message Dispatch:
– JS → Native: via WKScriptMessageHandler
– Native → JS: via evaluateJavaScript callbacks
• KKJSBridgeMessage / KKJSBridgeMessageDispatcher: parse and route messages
[Module Layer]
• ModuleRegister & ModuleMetaClass: register and instantiate modules
• Built-in modules: AjaxHook, AjaxProtocolHook, FetchHook, Cookie, FormData, JS utilities
• AJAX Delegate Manager: delegate interface to perform network tasks (NSURLSession or AFNetworking)
[Network & Storage]
• NSURLProtocol hook (for protocol‐based interception)
• Native NSURLSession for sending real requests and returning data to JS
• NSHTTPCookieStorage for cookie management
[JavaScript Layer]
• TS/rollup build output (KKJSBridgeAJAXHook.js, KKJSBridgeAJAXProtocolHook.js, other hooks)
• window.KKJSBridge API, hook scripts injected at page load
• Local/offline HTML/JS resources (demo server files)