JSPatch使用

原文链接

文章首发自点击打开链接

JSPatch 通过Objective-C 中的runtime将其与JavaScript链接起来。你可以通过仅仅引入一个小的机器来实现在JavaScript中调用任何Objective-C的类和方法。这将使APP获得脚本语言的有点:添加模块或者替换Objective-C代码来更快的修改bugs。

JSPatch仍在维护中,欢迎大家一起来完善这个项目.

注意:点击这里来获取完整的文档 

例子

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[JPEngine startEngine];
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script];

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[self.window addSubview:[self genView]];
[self.window makeKeyAndVisible];

return YES;
}
- (UIView *)genView
{
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
}

@end


// demo.js
require('UIView, UIColor, UILabel')
defineClass('AppDelegate', {
// replace the -genView method
genView: function() {
var view = self.ORIGgenView();
view.setBackgroundColor(UIColor.greenColor())
var label = UILabel.alloc().initWithFrame(view.frame());
label.setText("JSPatch");
label.setTextAlignment(1);
view.addSubview(label);
return view;
}
});

你还可以通过使用JSPatch Convertor来立即的将Objective-C转换到JavaScript.

安装
CocoaPods
CocoaPods 是一个Objective-C 的依赖管理器,具备使像JSpatch这种第三方库自动化、过程简明的安装到你的程序。查看开始引导更多的说明

# Your Podfile
platform :ios, '6.0'
pod 'JSPatch'

手动安装
拷贝 JSEngine.m JSEngine.h JSPatch.js in JSPatch/ to your project

使用
Objective-C

1.#import "JPEngine.h"
2.调用 [JPEngine startEngine]
3.使用JavaScript通过方法 [JPEngine evaluateScript:@""]
[JPEngine startEngine];

// exec js directly
[JPEngine evaluateScript:@"\
var alertView = require('UIAlertView').alloc().init();\
alertView.setTitle('Alert');\
alertView.setMessage('AlertView from js'); \
alertView.addButtonWithTitle('OK');\
alertView.show(); \
"];

// exec js file from network
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cnbang.net/test.js"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[JPEngine evaluateScript:script];
}];

// exec local js file
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script];

JavaScript
基本使用
//require
require('UIView, UIColor, UISlider, NSIndexPath')

// Invoke class method
var redColor = UIColor.redColor();

// Invoke instance method
var view = UIView.alloc().init();
view.setNeedsLayout();

// set proerty
view.setBackgroundColor(redColor);

// get property
var bgColor = view.backgroundColor();

// multi-params method (use underline to separate)
// OC:NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
var indexPath = NSIndexPath.indexPathForRow_inSection(0, 1);

// method name contains underline (use double undeline to represent)
// OC: [JPObject _privateMethod];
JPObject.__privateMethod()

// use .toJS() to convert NSArray / NSString / NSDictionary to JS type.
var arr = require('NSMutableArray').alloc().init()
arr.addObject("JS")
jsArr = arr.toJS()
console.log(jsArr.push("Patch").join('')) //output: JSPatch

// use hashes to represent struct like CGRect / CGSize / CGPoint / NSRange
var view = UIView.alloc().initWithFrame({x:20, y:20, width:100, height:100});
var x = view.bounds().x;

// wrap function with `block()` when passing block from JS to OC
// OC Method: + (void)request:(void(^)(NSString *content, BOOL success))callback
require('JPObject').request(block("NSString *, BOOL", function(ctn, succ) {
if (succ) log(ctn)
}));

// GCD
dispatch_after(1.0, function(){
// do something
})
dispatch_async_main(function(){
// do something
})

定义类
你可以重新定义一个已经存在的类或者重写方法
// OC
@implementation JPTableViewController
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *content = self.dataSource[[indexPath row]]; //may cause out of bound
JPViewController *ctrl = [[JPViewController alloc] initWithContent:content];
[self.navigationController pushViewController:ctrl];
}
- (NSArray *)dataSource
{
return @[@"JSPatch", @"is"];
}
- (void)customMethod
{
NSLog(@"callCustom method")
}
@end

// JS
defineClass("JPTableViewController", {
// instance method definitions
tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
var row = indexPath.row()
if (self.dataSource().count() > row) { //fix the out of bound bug here
var content = self.dataSource().objectAtIndex(row);
var ctrl = JPViewController.alloc().initWithContent(content);
self.navigationController().pushViewController(ctrl);
}
},

dataSource: function() {
// get the original method by adding prefix 'ORIG'
var data = self.ORIGdataSource().toJS();
return data.push('Good!');
}
}, {})

扩展
这有一些扩展提供支持自定义struct类型,C方法调用和一些其他方法,在启动引擎后通过调用addExtensions方法来添加拓展
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[JPEngine startEngine];

//add extensions after startEngine
[JPEngine addExtensions:@[@"JPInclude", @"JPCGTransform"]];

NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script];
}

@end
include('test.js') //include function provide by JPInclude.m
var view = require('UIView').alloc().init()

//CGAffineTransform is supported in JPCGTransform.m
view.setTransform({a:1, b:0, c:0, d:1, tx:0, ty:100})

推荐添加拓展的方式是通过js进行添加
require('JPEngine').addExtensions(['JPInclude', 'JPCGTransform'])

// `include()` and `CGAffineTransform` is avaliable now.
运行环境
1.iOS 7+, 不向前兼容 iOS 6
2.JavaScriptCore.framework
3.支持 armv7/armv7s/arm64

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值