Giter VIP home page Giter VIP logo

ctjsbridge's Introduction

CTJSBridge

An iOS bridge for sending messages between Obj-C and JavaScript in WKWebView

Features

  • Remote Debug
  • Send/Handle Mesasage From Native To JS
  • Send/Handle Mesasage From JS To Native

Play with Demo

  • You have to checkout that your mac has installed golang

  • cd into CTJSBridge/WebServer directory and make run

  • if you get listen tcp :80: bind: address already in use make: *** [run] Error 1 Error. Open then CTJSBridge/WebServer/main.go file and change the 80 to other port

  • Then pod update and run the CTJSBridge.xcworkspace project on simulator

  • When it is running, you will see the web page, and then tap five times on the screen, the connect info will show up, and the count is 0.

  • open Safari and go to http://localhost, enter connect info like:192.168.1.100:58931 to connect to simulator

  • close the connect info alert in simulator and tap five times on the screen to show it again, if count is 1, that means the safari has connected to your simulator.

  • play it!

  • in the real device, you do these step the same, remote debug works on real device either.

Usage

Make calls

see js call below:

      window.CTJSBridge.LoadMethod("Demo", "push", {"key":"value"}, {
        "success":function(result){
          document.getElementById('message').innerHTML = result;
        },
        "fail":function(result){
          document.getElementById('message').innerHTML = result;
        },
        "progress":function(result){
          document.getElementById('message').innerHTML = result;
        },
      });

CTJSBridge will alloc the Object named Target_H5Demo (Target_H5 + Demo), and call Action_push: (Action_ + push) with params {"key":"value"}. So you just make sure that you have a correct object and selector, CTJSBridge will call it!

the params should always be a dictionary.

In the params send to Action_push:, you will find three block named success,fail,progress,they received a NSDictionary as callback data to js.


if you are using CTNetworking to make API call in your app, just send the name of API Manager and API params, you call Native API from js on the fly! No extra works!

window.CTJSBridge.LoadAPI("CTMarvelCharactersAPIManager", {"orderBy":"modified"}, {
  "success":function(result){
    document.getElementById('message').innerHTML = result;
  },
  "fail":function(result){
    document.getElementById('message').innerHTML = result;
  },
  "progress":function(result){
    document.getElementById('message').innerHTML = result;
  },
});

Create WKWebview

create WKWebview with the category method:

#import <CTJSBridge/CTJSBridge.h>

- (WKWebView *)webview
{
    if (_webview == nil) {
        _webview = [WKWebView ct_WKWebViewWithConfiguration:nil prefixUserAgent:nil];
    }
    return _webview;
}

tada~ there you go!

see demo code for more detail:

calling from js: https://github.com/casatwy/CTJSBridge/blob/master/WebServer/static/index.html

objc receive code: https://github.com/casatwy/CTJSBridge/blob/master/CTJSBridge/Demo/Targets/Demo/Target_H5Demo.m

callback to js: https://github.com/casatwy/CTJSBridge/blob/master/CTJSBridge/Demo/DemoViewController.m#L47

Cocoapods

pod 'CTJSBridge'

Requirements

  • iOS 9 or higher

Authors

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

License

This project is licensed under the MIT License.

ctjsbridge's People

Contributors

casatwy avatar damonvvong avatar

Watchers

James Cloos avatar Tommy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.