iOS 通过 JSPatch 实时修复线上 bug!
2016-01-18 18:16
751 查看
JSPatch 是一个开源项目(Github链接),只需要在项目里引入极小的引擎文件,就可以使用 JavaScript 调用任何 Objective-C 的原生接口,替换任意 Objective-C 原生方法。目前主要用于下发 JS 脚本替换原生 Objective-C 代码,实时修复线上 bug。
除了实时修复线上 bug,甚至为 APP 动态添加一个模块也是可行的,不过可能会有性能问题。
使用JSPatch 需要有一个后台可以下发和管理脚本,并且需要处理传输安全等部署工作。
目前有一个JSPatch 平台提供了一系列的服务,只需引入一个 SDK 就能使用 JSPatch,只是还在内测中….
地址 :https://github.com/bang590/JSPatch
CocoPods安装:
Podfile文件
platform :ios, ‘6.0’
pod ‘JSPatch’
然后
pod install
下面开始使用:我们先创建一个列表,再通过JSPath改变行数
1.我们先在Controller里加入一个列表,让他显示3行 ,代码如下:
运行一下,显示3行,没问题
2.创建js文件 New File -> ios -> Empty …..
3.在js文件中 写入:
关于JSPath的具体使用,请自行查阅:https://github.com/bang590/JSPatch/wiki
4.调用这个js文件,代码如下:
运行一下,现在是显示10行了 !
**注:
如果我们想要通过 JS 脚本替换原生 Objective-C 代码,实时修复线上 bug的话,还是要通过服务器下发 js脚本 ! **
代码如下:
工具
将OC代码装换成JSPatch script
除了实时修复线上 bug,甚至为 APP 动态添加一个模块也是可行的,不过可能会有性能问题。
使用JSPatch 需要有一个后台可以下发和管理脚本,并且需要处理传输安全等部署工作。
目前有一个JSPatch 平台提供了一系列的服务,只需引入一个 SDK 就能使用 JSPatch,只是还在内测中….
地址 :https://github.com/bang590/JSPatch
CocoPods安装:
Podfile文件
platform :ios, ‘6.0’
pod ‘JSPatch’
然后
pod install
下面开始使用:我们先创建一个列表,再通过JSPath改变行数
1.我们先在Controller里加入一个列表,让他显示3行 ,代码如下:
#import "JRViewController.h" @interface JRViewController ()<UITableViewDataSource,UITableViewDelegate> @property(nonatomic,strong)UITableView* myTableView; @end @implementation JRViewController - (void)viewDidLoad { [super viewDidLoad]; UITableView* tv =[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain]; self.myTableView = tv; self.myTableView.delegate = self; self.myTableView.dataSource = self; [self.view addSubview:self.myTableView]; } #pragma mark -- UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 3; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString* i= @"cell"; UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:i]; if (cell == nil ) { cell =[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:i]; } cell.textLabel.text = @"title"; return cell; }
运行一下,显示3行,没问题
2.创建js文件 New File -> ios -> Empty …..
3.在js文件中 写入:
//将JRViewController类中的- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section方法替换掉 defineClass('JRViewController', { tableView_numberOfRowsInSection: function(tableView, section) { return 10; }, });
关于JSPath的具体使用,请自行查阅:https://github.com/bang590/JSPatch/wiki
4.调用这个js文件,代码如下:
#import "AppDelegate.h" #import "JPEngine.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [JPEngine startEngine]; NSString* path = [[NSBundle mainBundle]pathForResource:@"JSPathTest" ofType:@"js"]; NSString* js = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; [JPEngine evaluateScript:js]; return YES; }
运行一下,现在是显示10行了 !
**注:
如果我们想要通过 JS 脚本替换原生 Objective-C 代码,实时修复线上 bug的话,还是要通过服务器下发 js脚本 ! **
代码如下:
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://........"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [JPEngine evaluateScript:script]; }];
工具
将OC代码装换成JSPatch script
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Android学习笔记(二九):嵌入浏览器
- Android java 与 javascript互访(相互调用)的方法例子
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- JavaScript演示排序算法
- javascript实现10进制转为N进制数
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- 2019年开发人员应该学习的8个JavaScript框架
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- HTML中的script标签研究
- 对一个分号引发的错误研究
- 异步流程控制:7 行代码学会 co 模块
- ES6 走马观花(ECMAScript2015 新特性)
- JavaScript拆分字符串时产生空字符的原因