Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/BeamApp/Transit

Library to Bridge between JavaScript and iOS, OSX, Android
https://github.com/BeamApp/Transit

Last synced: 2 months ago
JSON representation

Library to Bridge between JavaScript and iOS, OSX, Android

Awesome Lists containing this project

README

        

# Transit [![Build Status](https://travis-ci.org/BeamApp/Transit.png)](https://travis-ci.org/BeamApp/Transit) [![Cocoa Pod](http://cocoapod-badges.herokuapp.com/p/Transit/badge.svg)](http://cocoadocs.org/docsets/Transit/) [![Cocoa Pod](http://cocoapod-badges.herokuapp.com/v/Transit/badge.svg)](http://cocoadocs.org/docsets/Transit/)

Transit bridges between JavaScript and iOS, OSX, Android. You can easily embed a WebView or scripted logic via JS into your application and pass in native code as functions and especially event handlers. Whenever your native code got called you can use all arguments as well as the `this` argument even if they are JavaScript, functions again! (some change before MTC presentation)

It does not rely on special JavaScript runtimes such as JavaScript Core or Rhino but can be used with any visual or non-visual component that understands JavaScript. That way, you can easily modify existing web pages to integrate with your native application (e.g. make `history.go(-1)` close the WebView or bind a button's `onClick` event to your native code). On the other hand, you can expose native functionality as accessible JavaScript functions (e.g. `[transit bind: toVariable:'navigator.vibrate']`).

## Example for Objective-C

Create a transit context from any (non-)visible webview

```objc
TransitUIWebViewContext *context = [TransitUIWebViewContext contextWithUIWebView:someViewView];
```

Evaluate JavaScript with convenient placeholders `@` and implicit type conversion. So, calling

```objc
NSLog(@"%@", [context eval:@" {result: @ + Math.max(23, @) } " val: @"foo" val: @42.5]);
```
prints `NSDictionary: { result: "foo42.5" }` to the console.

You can store JavaScript functions in native variables and call them later or pass them back to JavaScript at any time. This code

```objc
TransitFunction *mathMax = [context eval:@"Math.max"];
NSLog(@"%@", [mathMax callWithArg:@3.5 arg:@6] );
NSLog(@"%@", [context eval:@" @(3.5, @)" val: mathMax val:@6] );
```
prints `6` in both cases.

The same works the other way around, were you can pass blocks to JavaScript, e.g. to register or override global functions

```objc
context[@"log"] = ^(id object){
NSLog(@"%@", object);
};
[context eval:@"log('Hello, World!')"];
```

But the real strength of transit comes when you combine native code with JavaScript. Blocks or delegates can be called from JavaScript and can even receive JavaScript functions as arguments. This code snippet

```objc
TransitFunction *applyFunc = [context functionWithBlock:^(TransitFunction* func, float a, int b){
NSLog(@"arguments: func: %@, a: %f, b: %d", func, a, b);
return [func callWithArg:@(a) arg:@(b)];
}];

NSNumber* result = [context eval:@"@(Math.max, 3.5, @)" val:applyFunc val:@6];
NSLog(@"result: %f", result.floatValue);
```

outputs

```
arguments: func: , a: 3.500000, b: 6
result: 6.0000
```

to the console.

There's a lot more. e.g. `TransitCurrentCall` gives you access to the `this` variable, all `arguments` and let's you even print the unified call stack from JavaScript and native functions:

```
002 TransitNativeFunctionCallScope(this=(, 3.5, {field = 6;})
001 TransitEvalCallScope(this=) @(Math.max, 3.5, {field:@}) -- values:(, 6)
```

Read the [API documentation](http://cocoadocs.org/docsets/Transit/) for further details.

## Additional Information

You can find additional information in the Wiki, e.g.

* [Internal Architecture](https://github.com/BeamApp/Transit/wiki/Internals)
* [Android Internals](https://github.com/BeamApp/Transit/wiki/Android:-Communication)