# JavaScriptBridge ![License MIT](https://go-shields.herokuapp.com/license-MIT-yellow.png)
[![Version](https://cocoapod-badges.herokuapp.com/v/JavaScriptBridge/badge.png)](https://cocoapod-badges.herokuapp.com/v/JavaScriptBridge/badge.png)
[![Platform](https://cocoapod-badges.herokuapp.com/p/JavaScriptBridge/badge.png)](https://cocoapod-badges.herokuapp.com/p/JavaScriptBridge/badge.png)
[![Build Status](https://travis-ci.org/kishikawakatsumi/JavaScriptBridge.png?branch=master)](https://travis-ci.org/kishikawakatsumi/JavaScriptBridge)
[![Analytics](https://ga-beacon.appspot.com/UA-4291014-9/JavaScriptBridge/README.md)](https://github.com/igrigorik/ga-beacon)
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
```objc
#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.
```objc
JSContext *context = [JSBScriptingSupport globalContext];
```
2. Add `JSExports` adopted classes each framework if needed.
By default, `Foundation`, `UIKit`, `QuartzCore` frameworks are included.
```objc
[context addScriptingSupport:@"MapKit"];
[context addScriptingSupport:@"MessageUI"];
```
3. It is ready to use, writing appliction code and evaluate in JavaScript.
```objc
[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.
```objc
JSContext *context = [[JSContext alloc] init];
```
2. Add `JSExports` adopted classes each framework if needed.
`Foundation`, `UIKit` and `QuartzCore` frameworks **MUST** be added.
```objc
[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`
```objc
UILabel *label;
```
```javascript
var label;
```
**Properties**
Use dot syntax
```objc
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;
```
```javascript
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
```objc
UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
```
```javascript
var window = UIWindow.alloc().initWithFrame(UIScreen.mainScreen().bounds);
```
**Struct (CGRect, NSRange, etc.)**
Use Hashes
```objc
UIView *view = [UIView new];
view.frame = CGRectMake(20, 80, 280, 80);
CGFloat x = view.frame.origin.x;
CGFloat width = view.frame.size.width;
```
```javascript
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.
```objc
@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.
```objc
@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](https://github.com/kishikawakatsumi/JavaScriptBridge/tree/master/Examples/UICatalog/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**
```javascript
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**
```javascript
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.ac
徐浪老师
- 粉丝: 8529
- 资源: 1万+
最新资源
- 车用驱动电机原理与控制基础-P144公式(6-52)
- 小长轴半自动压入设备sw20可编辑全套技术资料100%好用.zip
- 小型全自动杀鱼机Creo4全套技术资料100%好用.zip
- 新型垃圾燃烧炉即垃圾处理设备sw17全套技术资料100%好用.zip
- 基于Javaweb的企业人事管理系统源码+数据库+文档说明.zip
- 腰果脱壳机Creo2全套技术资料100%好用.zip
- 基于PyQt5和ultralytics框架,支持导入模型,图片,视频,摄像头Based on PyQt5 and the ultralytics framework, it supports impo
- 基于集成模型的LSBoost算法的时间序列预测LSBoost matlab代码
- 基于粒子群(pso)优化的bp神经网络PID控制…
- 液晶面板 搬运抽检设备sw10可编辑全套技术资料100%好用.zip
- No.243 S7-200 MCGS 基于plc的自动加料机控制系统 243有 带解释的梯形图程序,接线图原理图图纸,io分配,组态画面
- 电压方程标幺化、PI标幺化、锁相环PLL标幺化 详解电机模型相关标幺化处理 电流环PI控制器的标幺化处理 观测器中PLL锁相环的标幺化处理 采样时间标幺化 这是文档
- 基于SpringBoot的物业管理系统【项目源码+数据库脚本】(高分毕设)
- 移动式液压配电泵sw17全套技术资料100%好用.zip
- COMSOL 燃料电池,冷启动仿真 低温质子交膜燃料电池冷启动仿真模型,cold start,可仿真包括冰的形成过程,温度分布,电流分布,物质浓度分布,速度压力分布以及膜中水分布,可提供相关方面仿真建
- 油墨脱泡机 真空脱泡机sw18可编辑全套技术资料100%好用.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈