Coder Social home page Coder Social logo

joechenfc / hybridbridge Goto Github PK

View Code? Open in Web Editor NEW

This project forked from liaojinxing/hybridbridge

0.0 2.0 0.0 767 KB

A basic framework for iOS hybrid development, including web resource management, and bridge between JavaScript and ObjC.

Home Page: http://liaojinxing.github.io/%E6%B7%B7%E5%90%88%E5%BC%80%E5%8F%91%E5%AE%9E%E8%B7%B5/

License: Other

Objective-C 3.63% CSS 3.22% JavaScript 92.92% HTML 0.11% Ruby 0.11%

hybridbridge's Introduction

HybridBridge

This library provides a basic framework for iOS hybrid development.

It provides:

  • Template resource(html/js/css) management.

  • Bridge for coding with JS and Objc, using JavaScriptCore.Framework.

    • send message from JavaScript to native, and callback after native responsed.

    The data flows: Javascript --> JavaScriptCore --> Native --> JavaScriptCore --> Javascript

    • Call JavaScript function in native, and callback after Javascript responsed.

    The data flows: Native --> JavaScriptCore --> JavaScript --> JavaScriptCore --> Native

    • Native send message to JavaScript, such as pushing notification.

With this library, you can communicate your native codes with web codes easily.

Hybrid Application

Hybrid apps are part native apps, part web apps. Like native apps, they live in an app store and can take advantage of the many device features available. They rely on HTML being rendered in UIWebViews, so as to avoid publish new versions for some updates. More details in this Blog.

Installation

  • Grab the source file into your project.
  • Or use cocoapods. Cocoapods is really great. Here is an example of your podfile:
pod 'HybridBridge'

JavaScript and Objc bridge

Inherite BridgeWebViewController for your custom controller

@interface MainViewController : BridgeWebViewController

And now you can communicate between js and objc.

JavaScript driven

There are two ways for JavaScript to drive to communication.

  • Send message and callback

In your js code, you can use sendMessageAndCallback to send message from js to objc and callback after the send operation succeeds.

bridge.sendMessageAndCallback(eventType, message, callbackFunction)

Native driven

On the other hand, native can call JavaScript codes. After you register handler in JavaScript, you can call this handler in native side now.

JavaScript register handler:

bridge.registerHandler(handlerName, handler)

Native call handler:

[self callHandler:@"handler" parameters:@[] callback:^(id responseData) {
  // process with response data from js
}];

Notification

Native push notification:

[self sendMessageToJSForKey:eventType value:messages];

JavaScript receive message:

bridge.receiveMessage(eventType, callback) 

Some Common Usage

  • Push ViewController.
bridge.pushController(url, name)    

The name is used to route controller in navite end.

  • Get JSON:
bridge.getJSON(url, options, callback)

Template Resource management

In hybrid app, in order to improve the performance, we always hold the template resources(html/js/css/img) locally.

Firstly, create initial resources for the first time in AppDelegate:

#import "WebBridgeAPI.h"

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  // Override point for customization after application launch.

  ResourceManager *manager = [ResourceManager sharedManager];
  [manager createInitialResource];
  return YES;
}

Check resource update and download if needed:

For more details, please check the example project.

hybridbridge's People

Contributors

liaojinxing avatar jexcellent avatar

Watchers

James Cloos avatar  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.