Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bbc6bae9/javascript-bridge
About An iOS bridge for sending messages between Obj-C and JavaScript in JSContext / WebViews.
https://github.com/bbc6bae9/javascript-bridge
ios javascript javascript-applications jsbridge web webview
Last synced: 28 days ago
JSON representation
About An iOS bridge for sending messages between Obj-C and JavaScript in JSContext / WebViews.
- Host: GitHub
- URL: https://github.com/bbc6bae9/javascript-bridge
- Owner: BBC6BAE9
- License: mit
- Created: 2022-04-29T04:08:04.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-05-02T16:12:17.000Z (over 2 years ago)
- Last Synced: 2024-11-25T04:07:21.169Z (28 days ago)
- Topics: ios, javascript, javascript-applications, jsbridge, web, webview
- Language: Objective-C
- Homepage:
- Size: 19.4 MB
- Stars: 6
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# HWJavaScriptBridge
[![CI Status](https://img.shields.io/travis/bbc6bae9/HWJavaScriptBridge.svg?style=flat)](https://travis-ci.org/bbc6bae9/HWJavaScriptBridge)
[![Version](https://img.shields.io/cocoapods/v/HWJavaScriptBridge.svg?style=flat)](https://cocoapods.org/pods/HWJavaScriptBridge)
[![License](https://img.shields.io/cocoapods/l/HWJavaScriptBridge.svg?style=flat)](https://cocoapods.org/pods/HWJavaScriptBridge)
[![Platform](https://img.shields.io/cocoapods/p/HWJavaScriptBridge.svg?style=flat)](https://cocoapods.org/pods/HWJavaScriptBridge)![bridge](./bridge.gif)
## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
![bridge](./bridge.png)
## Requirements
iOS 6.0+
## Installation
HWJavaScriptBridge is available through [CocoaPods](https://cocoapods.org). To install
it, simply add the following line to your Podfile:```ruby
pod 'HWJavaScriptBridge'
```## Usage
### For WKWebView
```objective-c
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":@"tencent"}
};
responseCallback(clientData);
}];
```
```objective-c
[self.bridge callHandler:@"dispatchEvent" data:@{@"func": @"preRender"} responseCallback:^(id _Nonnull responseData) {
NSLog(@"[preRender] callback =%@", responseData);
}];
```
### For JSContext```objective-c
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":@"tencent"}
};
responseCallback(clientData);
}];
``````objective-c
// 客户端主动调用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.