Latest 0.1.0
Homepage https://github.com/JianweiWangs/Chopper
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

Example

use

git clone https://github.com/JianweiWangs/Chopper.git
cd Chopper
make

to fetch and build source code quickly.

To run the example project, clone the repo, and run make from the root directory first.

There are some script help you develop and PR.

# install dependence and open project
make

# install dependence
make install

# build test
make test

# open project
make open

# quit Xcode
make quit

Before you pull request, make sure test success.

Usage

Quick Start

Recommended

  1. create a javascript module

    import Chopper
    class TestModule: JavaScriptModuleInterface {
    
    // test module
    var module: String {
        return "test"
    }
    // actions, you can add any action you want
    var moduleMapping: [String : Dispatch] {
        return [
            "showAlert" : showAlert
        ]
    }
    
    func showAlert(message: JavaScriptMessage, callback: @escaping (Bool, [String : Any]) -> Void) {
        print(message.context.frameViewController)
        print(message.context.viewController)
        print(message)
        callback(true, ["code" : "0"])
    }
    }
  2. create bridge, inject module to dataSource
import Chopper
class ViewController: UIViewController  {

    @IBOutlet weak var webview: WKWebView!

    var jsbridge: JavaScriptBridge!

    override func viewDidLoad() {
        super.viewDidLoad()

        jsbridge = JavaScriptBridge(dataSource: self)

    }
}

extension ViewController: JavaScriptBridgeDataSource {

    // you can return multiple module instance, the modules more there are, the more actions can be handle
    var modules: [JavaScriptModuleInterface] {
        return [TestModule()]
    }

    var viewController: UIViewController {
        return self
    }

    var webView: WKWebView {
        return self.webview
    }

}
  1. JavaScript Import it
    
    dispatch('test', 'showAlert', {
    'title': 'Chopper',
    'message': 'this is a js call native test'
    }, function (success, params) {
    alert('callback isSuccess: ' + success + ' params: ' + params.code)
    })

You also call use convenience regist to bridge webview script

```swift
jsbridge.regist(module: "convenience", action: "showAlert") { [unowned self] (message, callback) in
    //  recommand use message.context.viewController instead of `self`
    //  if use self, use weak or unowned to avoid reference cycle
    print(message.context.viewController == self) //  true
    print(message)
    callback(true, ["code": 1])
}

Requirements

Swift 4.0, iOS 8.0

Installation

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

pod 'Chopper'

Author

JianweiWangs, [email protected]

License

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

Latest podspec

{
    "name": "Chopper",
    "version": "0.1.0",
    "summary": "Chopper is a light weight hybrid bridge.",
    "description": "Chopper is a light weight hybrid bridge, use it can build a hybrid app simple and quickly.",
    "homepage": "https://github.com/JianweiWangs/Chopper",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "JianweiWangs": "[email protected]"
    },
    "source": {
        "git": "https://github.com/JianweiWangs/Chopper.git",
        "tag": "0.1.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "swift_version": "4.2",
    "default_subspecs": "Core",
    "subspecs": [
        {
            "name": "Core",
            "source_files": "Chopper/Classes/Core/**/*.swift"
        },
        {
            "name": "Interceptor",
            "source_files": "Chopper/Classes/Interceptor/**/*.swift",
            "platforms": {
                "ios": "11.0"
            },
            "dependencies": {
                "Chopper/Broswer": []
            }
        },
        {
            "name": "Broswer",
            "source_files": "Chopper/Classes/Broswer/**/*.swift",
            "dependencies": {
                "Chopper/Core": []
            }
        }
    ]
}

Pin It on Pinterest

Share This