简介

JSPatch 可以让你用 JavaScript 书写原生 iOS APP。只需在项目引入极小的引擎,就可以使用 JavaScript 调用任何 Objective-C 的原生接口,获得脚本语言的优势:为项目动态添加模块,或替换项目原生代码动态修复 bug。

优势

  • 在项目中引入JSPatch,就可以在发现bug时下发JS脚本替换原生方法,可以做到无需更新整个APP即时修复bug!

  • JSPatch用iOS内置的 JavaScriptCore.framework作为引擎;JSPatch也符合苹果的规则。苹果不允许动态下发可执行代码,但通过苹果 JavaScriptCore.framework 或 WebKit 执行的代码除外,JS 正是通过 JavaScriptCore.framework 执行的。

  • 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', {
// 替换这个 -genView 方法
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;
}
});

安装

通过Cocopods安装

pod 'JSPatch' # 在线更新应用.

手动导入

  1. 下载https://github.com/bang590/JSPatch并解压

  2. 复制JSPatch文件夹到你的工程

使用

objective-C:

  1. 导入头文件#import "JPEngine.h"

  2. 导入本地JS(demo.js)见文首github示例demo(可选,实际项目中,根据自己实际需要进行.)

  3. 调用[JPEngine startEngine] 加载引擎

  4. 通过[JPEngine evaluateScript:@""]接口执行 JavaScript。

[JPEngine startEngine];

// 直接执行js
[JPEngine evaluateScript:@"\
var alertView = require('UIAlertView').alloc().init();\
alertView.setTitle('Alert');\
alertView.setMessage('AlertView from js'); \
alertView.addButtonWithTitle('OK');\
alertView.show(); \
"]; // 从网络拉回js脚本执行
[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];
}]; // 执行本地js文件
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script];
// 另一个例子

// 加载引擎
[JPEngine startEngine]; // 本地JS,动态更新技术就是通过服务器获取JS更新这个JS
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];
NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];
[JPEngine evaluateScript:script]

JavaScript:

基础使用方式

// 调用require引入要使用的OC类
require('UIView, UIColor, UISlider, NSIndexPath') // 调用类方法
var redColor = UIColor.redColor(); // 调用实例方法
var view = UIView.alloc().init();
view.setNeedsLayout(); // set proerty
view.setBackgroundColor(redColor); // get property
var bgColor = view.backgroundColor(); // 多参数方法名用'_'隔开:
// OC:NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
var indexPath = NSIndexPath.indexPathForRow_inSection(0, 1); // 方法名包含下划线'_',js用双下划线表示
// OC: [JPObject _privateMethod];
JPObject.__privateMethod() // 如果要把 `NSArray` / `NSString` / `NSDictionary` 转为对应的 JS 类型,使用 `.toJS()` 接口.
var arr = require('NSMutableArray').alloc().init()
arr.addObject("JS")
jsArr = arr.toJS()
console.log(jsArr.push("Patch").join('')) //output: JSPatch // 在JS用字典的方式表示 CGRect / CGSize / CGPoint / NSRange
var view = UIView.alloc().initWithFrame({x:20, y:20, width:100, height:100});
var x = view.bounds.x; // block 从 JavaScript 传入 Objective-C 时,需要写上每个参数的类型。
// 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(function(1.0, function(){
// do something
}))
dispatch_async_main(function(){
// do something
})

详细文档请参考wiki页面:基础用法

定义类/替换方法

defineClass() 定义 Objective-C 的类,对类和实例方法进行动态替换。

// 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!');
}
}, {})

详细文档请参考wiki页面:defineClass的用法

扩展

一些自定义的struct类型、C函数调用以及其他功能可以通过扩展实现,调用 +addExtensions: 可以加载扩展接口:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[JPEngine 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];
}
include('test.js')   //`include()`方法在扩展 JPInclude.m 里提供
var view = require('UIView').alloc().init() //struct CGAffineTransform 类型在 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.

可以通过新增扩展为自己项目里的 struct 类型以及C函数添加支持,详情请见wiki页面:添加新扩展

安全性

JSPatch非常强大,因而最好将通过服务器获取JS的链接进行加密,本地JS也最好加密处理


注: 文章由我们 iOS122 的小伙伴 @偌一茗 整理,喜欢就一起参与: iOS122 任务池

最新文章

  1. 【转】浅析Sql Server参数化查询
  2. [JS] JavaScript由浅入深(2) 进阶
  3. bzoj 2257: [Jsoi2009]瓶子和燃料
  4. org.apache.hadoop.conf-Configured
  5. Spring Data JPA教程, 第六部分: Sorting(未翻译)
  6. 【无聊放个模板系列】BZOJ 3172 (AC自动机)
  7. AVAudioRecorder 录制音频
  8. iOS开发中遇到的bug
  9. Django 基础二(View和urls)
  10. Android--app性能问题的总结(一)
  11. luffy项目后台drf搭建(1)
  12. vue-cli的跨域配置(自己总结)
  13. Windows下MongoDB设置用户、密码
  14. [python] A*算法基于栅格地图的全局路径规划
  15. python实现简单的定时任务
  16. [TensorBoard] *Cookbook - Tensorboard
  17. golang 如何查看channel通道中未读数据的长度
  18. 【转】CSS和SVG中的剪切——clip-path属性和<clipPath>元素
  19. git bash安装和基本设置
  20. 13、Redis的发布订阅模式

热门文章

  1. 性能测试工具LoadRunner03-LR之Virtual User Generator 脚本创建以及回放设置
  2. 错误 chamfermatching.cpp:969:30: error: the compiler can assume that the address of ‘annotate_img’
  3. pat1008. Elevator (20)
  4. .NET面试题1
  5. Java基础入门 - 关键字及其分类和说明
  6. python的数字
  7. FastReport Site授权联合推广计划 彻底保障商业化开发,还送iPhone 5s
  8. Andoid Intent学习之在各个活动之间传递数据
  9. 《ArcGIS Runtime SDK for Android开发笔记》——(2)、Android Studio基本配置与使用
  10. django orm 时间字段讲解