GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - BBC6BAE9/javascript-bridge: 支持JSContext、WebView双环境的JSBridge

BranchesTags
Open more actions menu

Latest commit

 

History

15 Commits
15 Commits

Folders and files

NameName
Last commit message
Last commit date
Example
Example
 
 
HWJavaScriptBridge
HWJavaScriptBridge
 
 
LICENSE
LICENSE
 
 
Package.swift
Package.swift
 
 
README.md
README.md
 
 

Repository files navigation

HWJavaScriptBridge

Version License Platform

Example

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

Requirements

iOS 6.0+

Installation

CocoaPods

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

pod 'HWJavaScriptBridge'

Swift Package Manager

You can also use Swift Package Manager to install HWJavaScriptBridge. Add the following dependency to your Package.swift:

dependencies: [
    .package(url: "https://github.com/bbc6bae9/HWJavaScriptBridge.git", from: "1.0.0")
]

Or add it directly in Xcode:

  1. Go to File → Add Package Dependencies
  2. Enter the repository URL: https://github.com/bbc6bae9/HWJavaScriptBridge.git
  3. Select the version you want to use

Usage

For WKWebView

 HWWebViewJavaScriptBridge *bridge = [[HWWebViewJavaScriptBridge alloc] initWithWebView:self.webView];
    self.bridge = bridge;
    [bridge registerHandler:@"invoke" handler:^(id  _Nonnull data, HWJBResponseCallback  _Nonnull responseCallback) {
        NSDictionary *clientData = @{
            @"code":@(0),
            @"msg":@"success",
            @"data":@{@"company":@"Fooman"}
        };
        responseCallback(clientData);
    }];
 [self.bridge callHandler:@"dispatchEvent" data:@{@"func": @"preRender"} responseCallback:^(id  _Nonnull responseData) {
        NSLog(@"[preRender] callback =%@", responseData);
    }];

For JSContext

    JSVirtualMachine *vm = [[JSVirtualMachine alloc] init];
    JSContext *ctx = [[JSContext alloc] initWithVirtualMachine:vm];
    self.ctx  = ctx;
    HWJSContextJavaScriptBridge *bridge = [[HWJSContextJavaScriptBridge alloc] initWithJSContext:ctx];
    self.bridge = bridge;
    
    // 监听JS调用的postMessage方法
    [bridge registerHandler:@"invoke" handler:^(id  _Nonnull data, HWJBResponseCallback  _Nonnull responseCallback) {
        NSDictionary *clientData = @{
            @"code":@(0),
            @"msg":@"success",
            @"data":@{@"company":@"Fooman"}
        };
        responseCallback(clientData);
    }];
 // 客户端主动调用JS,并得到JS的回应
    NSDictionary *data = @{
        @"func":@"preRender",
        @"params":@{
            @"url":@"https://m.film.qq.com"
        }
    };
    [self.bridge callHandler:@"dispatchEvent" data:data responseCallback:^(id responseData) {
        NSLog(@"[preRender] recieve callback data=%@", responseData);
    }];

Author

bbc6bae9, [email protected]

License

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

About

支持JSContext、WebView双环境的JSBridge

Topics

iosjavascriptjavascript-applicationsjsbridgewebwebview

Resources

Readme
MIT license
Activity

Stars

6 stars

Watchers

1 watching

Forks

2 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.