• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kishikawakatsumi/JavaScriptBridge: Write iOS apps in Javascript! JavaScriptBridg ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

kishikawakatsumi/JavaScriptBridge

开源软件地址:

https://github.com/kishikawakatsumi/JavaScriptBridge

开源编程语言:

Objective-C 97.5%

开源软件介绍:

JavaScriptBridge License MIT

Version Platform Build Status Analytics

Write iOS apps in Javascript! JavaScriptBridge provides the way to write iOS apps with JavaScript. JavaScriptBridge bridges Cocoa touch to JavaScriptCore (JavaScriptCore.framework is introduced in iOS 7).

You get the power of dynamics of scripting language for your apps.

It is still in development, obviously. You're welcomed to contribute if you find the project interesting!

Usage

#import <JavaScriptBridge/JavaScriptBridge.h>
...

// Retrieve the prepared context
JSContext *context = [JSBScriptingSupport globalContext];

// Add framework support if needed.
// ('Foundation', 'UIKit', 'QuartzCore' enabled by default.)
[context addScriptingSupport:@"MapKit"];
[context addScriptingSupport:@"MessageUI"];

// Evaluate script
[context evaluateScript:
 @"var window = UIWindow.new();"
 @"window.frame = UIScreen.mainScreen().bounds;"
 @"window.backgroundColor = UIColor.whiteColor();"
 @"window.makeKeyAndVisible();"
];
  1. Retrieve the JSContext instance from JSBScriptingSupport. The context includes a lot of system classes that has been JSExports adopted.
JSContext *context = [JSBScriptingSupport globalContext];
  1. Add JSExports adopted classes each framework if needed. By default, Foundation, UIKit, QuartzCore frameworks are included.
[context addScriptingSupport:@"MapKit"];
[context addScriptingSupport:@"MessageUI"];
  1. It is ready to use, writing appliction code and evaluate in JavaScript.
[context evaluateScript:
@"var window = UIWindow.new();"
@"window.frame = UIScreen.mainScreen().bounds;"
@"window.backgroundColor = UIColor.whiteColor();"
@"window.makeKeyAndVisible();"
];

Manually setting up a new JSContext instance

  1. Create new JSContext instance instead using globalContext. You can separate JavaScript environments to use multiple contexts.
JSContext *context = [[JSContext alloc] init];
  1. Add JSExports adopted classes each framework if needed. Foundation, UIKit and QuartzCore frameworks MUST be added.
[context addScriptingSupport:@"Foundation"];
[context addScriptingSupport:@"UIKit"];
[context addScriptingSupport:@"QuartzCore"];
[context addScriptingSupport:@"Accounts"];
[context addScriptingSupport:@"Social"];

Syntax / Naming conventions

Class name

Same as Objective-C

Variable declaration

Get rid of Type name instead use var

UILabel *label;
var label;

Properties

Use dot syntax

UISlider *slider = [[UISlider alloc] initWithFrame:frame];
slider.backgroundColor = [UIColor clearColor];
slider.minimumValue = 0.0;
slider.maximumValue = 100.0;
slider.continuous = YES;
slider.value = 50.0;
var slider = UISlider.alloc().initWithFrame(frame);
slider.backgroundColor = UIColor.clearColor();
slider.minimumValue = 0.0;
slider.maximumValue = 100.0;
slider.continuous = true;
slider.value = 50.0;

Invoking method

Use dot syntax All colons are removed from the selector Any lowercase letter that had followed a colon will be capitalized

UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
var window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds);

Struct (CGRect, NSRange, etc.)

Use Hashes

UIView *view = [UIView new];
view.frame = CGRectMake(20, 80, 280, 80);

CGFloat x = view.frame.origin.x;
CGFloat width = view.frame.size.width;
var view = UIView.new();
view.frame = {x: 20, y: 80, width: 280, height: 80};

var x = view.frame.x; // => 20
var width = view.frame.width; // => 280

Examples

###Hello world on JavaScriptBridge

This is the most simplest way.

@implementation JSBAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    JSContext *context = [JSBScriptingSupport globalContext];
    
    [context evaluateScript:
     @"var window = UIWindow.new();"
     @"window.frame = UIScreen.mainScreen().bounds;"
     @"window.backgroundColor = UIColor.whiteColor();"
     @""
     @"var navigationController = UINavigationController.new();"
     @"var viewController = UIViewController.new();"
     @"viewController.navigationItem.title = 'Make UI with JavaScript';"
     @""
     @"var view = UIView.new();"
     @"view.backgroundColor = UIColor.redColor();"
     @"view.frame = {x: 20, y: 80, width: 280, height: 80};"
     @""
     @"var label = UILabel.new();"
     @"label.backgroundColor = UIColor.blueColor();"
     @"label.textColor = UIColor.whiteColor();"
     @"label.text = 'Hello World.';"
     @"label.font = UIFont.boldSystemFontOfSize(24);"
     @"label.sizeToFit();"
     @""
     @"var frame = label.frame;"
     @"frame.x = 10;"
     @"frame.y = 10;"
     @"label.frame = frame;"
     @""
     @"view.addSubview(label);"
     @"viewController.view.addSubview(view);"
     @""
     @"navigationController.viewControllers = [viewController];"
     @""
     @"window.rootViewController = navigationController;"
     @"window.makeKeyAndVisible();"
     ];
    
    return YES;
}

@end

Of course, the script is able to be loaded from external file.

@implementation JSBAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *path = [mainBundle pathForResource:@"main" ofType:@"js"];

    NSString *script = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    JSContext *context = [JSBScriptingSupport globalContext];
    [context evaluateScript:script];
    
    return YES;
}

@end

###Writing apps with only JavaScript

See the UICatalog example.

Enhancements

###Define custom classes

You can define custom class in JavaScript. It is needs to interact system provided framework.

JSB.defineClass(declaration, instanceMembers, staticMembers) function defines Objective-C class in JavaScript. Pass the class declaration string to first argument.

Second argument is instance method definitions as hash. The hash object inclueds function object, each keys are to be used as method name.

Example

var MainViewController = JSB.defineClass('MainViewController : UITableViewController', {
  // Instance Method Definitions
  viewDidLoad: function() {
    self.navigationItem.title = 'UICatalog';
  },
  viewWillAppear: function(animated) {
    self.tableView.reloadData();
  }
}, {
  // Class Method Definitions
  attemptRotationToDeviceOrientation: function() {
    ...
  }
});

Example

var MainViewController = JSB.defineClass('MainViewController : UITableViewController <UITableviewDataSource, UITableviewDelegate>', // Declaration
// Instance Method Definitions
{
  viewDidLoad: function() {
    self.navigationItem.title = 'UICatalog';
  },
  tableViewNumberOfRowsInSection: function(tableView, section) {
    return self.menuList.length;
  },
  tableViewCellForRowAtIndexPath: function(tableView, indexPath) {
    var cell = UITableViewCell.alloc().initWithStyleReuseIdentifier(3, 'Cell');
    cell.accessoryType = 1;
    cell.textLabel.text = self.menuList[indexPath.row]['title'];
    cell.detailTextLabel.text = self.menuList[indexPath.row]['explanation'];

    return cell;
  },
  tableViewDidSelectRowAtIndexPath: function(tableView, indexPath) {
    var targetViewController = self.menuList[indexPath.row]['viewController'];
    self.navigationController.pushViewControllerAnimated(targetViewController, true);
  }
},
{
// Class Method Definitions
  ...
});

###Modules

JavaScriptBridge provides simple module system require/exports funcitons, like Node.js.

JSB.require(name) function enables external module, JSB.exports publishes a module.

See example.

var ButtonsViewController = JSB.require('buttonsViewController');
var ControlsViewController = JSB.require('controlsViewController');
var WebViewController = JSB.require('webViewController');
var MapViewController = JSB.require('mapViewController');

var MainViewController = JSB.defineClass('MainViewController : UITableViewController', {
  viewDidLoad: function() {
    self.navigationItem.title = 'UICatalog';
    ...
    
});

JSB.exports = MainViewController;

###For Debug JSB.log function is the same as NSLog.

JSB.log('view: %@', self.view);

Requirements

  • iOS 7 or later
  • JavaScriptCore.framework

Installation

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

pod "JavaScriptBridge"

Author

kishikawa katsumi, [email protected]

License

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap