iOS- JSPatch 热更新
即便现在苹果努力优化他的审核部门,将审核速度提升到尽可能不可思议的一天或两天通过审核,但是对于想随意控制app实时更新的公司而言,这显示是不够的,热更新应运而生..
JSPatch
-> JSPatch github 链接
也许也有很多人觉得这是个很高大上的技术,其实不然,我们只需要在项目里引入极小的引擎文件,就可以使用 JavaScript 调用任何 Objective-C 的原生接口,替换任意 Objective-C 原生方法。当然呢,目前主要用于下发 JS 脚本替换原生 Objective-C 代码,实时修复线上 bug。
JSPatch实现的内部原理
JsPatch能做到通过JS调用和改写OC方法最根本的原因是 Objective-C 是动态语言,OC上所有方法的调用/类的生成都通过 Objective-C Runtime 在运行时进行,我们可以通过类名和方法名反射得到相应的类和方法,也可以替换某个类的方法为新的实现,还可以新注册一个类,为类添加方法。这里就不详细介绍runtime了。所以 JSPatch 的原理就是:JS传递字符串给OC,OC通过 Runtime 接口调用和替换OC方法。
[JSPatch官方文档 - 作者bang]
https://github.com/bang590/JSPatch/wiki
[JSPatch原理总结 - 作者bang]
https://github.com/bang590/JSPatch/wiki/JSPatch-%E5%AE%9E%E7%8E%B0%E5%8E%9F%E7%90%86%E8%AF%A6%E8%A7%A3
[大牛对JSPatch的整理]
http://www.jianshu.com/p/822ba5141833
[大牛对JSPatch的使用方法总结]
http://www.jianshu.com/p/2171b51faf4d
下面是我使用JSPatch的使用过程
新建一个项目,项目中代码实现如下
- (void)viewDidLoad {[super viewDidLoad];[self loadDemoTableVC];
}-(void)loadDemoTableVC{UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];tableView.delegate = self;tableView.dataSource = self;[self.view addSubview:tableView];self.demoTableVC = tableView;
}#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 18;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kID];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kID];}cell.backgroundColor = indexPath.row % 2 == 0 ? [UIColor lightGrayColor] : [UIColor yellowColor];return cell;
}
运行效果为黄灰相间的tableView
下面使用JSPatch进行热更新,
修改后的OC代码为
-(void)loadDemoTableVC{UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];tableView.delegate = self;tableView.dataSource = self;[self.view addSubview:tableView];self.demoTableVC = tableView;}#pragma mark - UITableViewDataSource- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{return 8;}-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 2;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kID];if (!cell) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kID];}cell.backgroundColor = indexPath.row % 2 == 0 ? [UIColor greenColor] : [UIColor redColor];return cell;}
将其修改为红绿相间的,Group格式的tableView
OC实现相应功能是最基本的技能
使用过程最关键的OC 转化为 JSPatch支持的JS代码
1.使用 JSPatch 作者开源的转化工具进行简单转化
JSPatchConvertor转化工具
转化后的JS代码并不能直接在项目中使用,因为OC中的很多代码需要通过特定的格式进行处理
OC代码转化JS代码协议
修改后的JS代码
require('UITableView,UITableViewCell,UIColor');
defineClass('ViewController',{loadDemoTableVC: function() {var tableView = UITableView.alloc().initWithFrame_style(self.view().bounds(), 1);tableView.setDelegate(self);tableView.setDataSource(self);self.view().addSubview(tableView);self.setDemoTableVC(tableView);
}})defineClass('ViewController',{tableView_numberOfRowsInSection: function(tableView, section) {return 8;
},
numberOfSectionsInTableView: function(tableView) {return 2;
},
tableView_cellForRowAtIndexPath: function(tableView, indexPath) {var cell = tableView.dequeueReusableCellWithIdentifier("kID");if (!cell) {cell = require('UITableViewCell').alloc().initWithStyle_reuseIdentifier(0, "kID");}cell.setBackgroundColor(indexPath.row() % 2 == 0 ? UIColor.greenColor() : UIColor.redColor());return cell;
}})
万事俱备只欠使用->
1.导入两个必要的框架
libz.1.tbz
JaveScriptCore.framework
2.导入框架
3.在AppDelegate导入头文件 #import "JPEngine.h"
中并实现
[JPEngine startEngine];NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"JSPathDemo" ofType:@"js"];NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];[JPEngine evaluateScript:script];
这里JS文件先放在本地进行测试,真实开发环境需要从服务器请求JS文件
之后是自己配置服务器分发JS还是使用 jspatch开放平台 任意,开放平台文档很全,在此就不再赘述
项目演示Demo: Github Github_CSDNDemos中JSPatchDemo
iOS- JSPatch 热更新相关推荐
- IOS 2020 热更新
先夸夸JSPatch,平台 SDK 1.7.2 以上版本可以通过苹果审核,可以正常使用.Github 开源版本暂不可用. 暂时1.8.0 是需要付费使用的.还需要联系获得,跟阿里的热更新差不多. 阿里 ...
- Unity为什么ios不能热更新
unity的c#代码运行的方式以及可以通过拆分代码模块,将可能需要更新扩展的逻辑放到一个独立的.dll中,通过更新这个.dll文件来实现热更新. 但是这种朴素的想法在ios平台会受到限制. c#编译出 ...
- yooasset+hybridclr在android,ios端热更新测试
Hybridclr+YooAsset+Unity Run android+iOS 这个工程是用来学习YooAsset和Hybridcl,来做unity资源和代码热更新, 实现了android 和ios ...
- iOS之热更新/热修复JSPatch 补丁更新管理
总结: 1.在传输过程中对JS文件进行了RSA签名加密: 2.下载完脚本保存到本地时,应进行简单的对称加密,每次读取时解密: 3.建议js脚本的增.删.改.查的内容在同一个js文件处理,只下载一个文件 ...
- iOS JSPatch 热修复使用
概述 一说到热修复,可能很多人会觉得应该很复杂,很难用(我以前是这么觉得的...),实际使用起来蛮简单的,这里以一个小demo演示热修复是如何修复崩溃的,具体更深入的用法,可以看这个 https:// ...
- JSPatch 热更新
JSPatch 是一个 iOS 动态更新框架,只需在项目中引入极小的引擎,就可以使用 JavaScript 调用任何 Objective-C/Swift 原生接口. 获得脚本语言的优势,为项目动态添加 ...
- 移动端APP热更新方案(iOS+Android)
出自:http://www.cnblogs.com/Creator/p/7007694.html 为什么要做热更新 当一个App发布之后,突然发现了一个严重bug需要进行紧急修复,这时候公司各方就会忙 ...
- iOS热更新实现方式
转发 链接:https://www.jianshu.com/p/00111d447e7e 苹果静止热更新,可惜我的是企业app,没有这些约束了,随便用.(当然有些热更新已经可以通过苹果审核了,比如JS ...
- iOS 热更新8种实现方式
一.JSPatch 热更新时,从服务器拉去js脚本.理论上可以修改和新建所有的模块,但是不建议这样做. 建议 用来做紧急的小需求和 修复严重的线上bug. 二.lua脚本 比如: wax.热更新时,从 ...
- 移动端热更新方案(iOS+Android)
PPT资源包含iOS+Android 各种方案分析:https://github.com/qiyer/Share/blob/master/%E7%83%AD%E6%9B%B4%E6%96%B0%E5% ...
最新文章
- 使用Memcached实现Session共享
- Code:blocks 调试经历,调试成功这其一
- 中科大刘和刚老师的计算机图形学资料 for students
- 基于python的系统构建_搭建一个基于python的深度学习环境
- leetcode167. 两数之和 II - 输入有序数组
- 日志存储 elasticsearch vs clickhouse
- 【leetcode】排序题(python)
- lstm网络一般训练多少轮_Pytorch的LSTM的理解
- 兰州中川机场停车费一天多少钱,中川机场附近停车便宜
- 【前端 教程】详解 立即执行函数
- java IO流学习总结
- redis trib 常用命令
- PythonTip挑战题(16-25)
- 扫描识别工具Dynamic Web TWAIN使用教程:移动浏览器捕获(上)
- 失眠怎么办?试试汤臣倍健褪黑素和以下方法!
- 省级交通运输行政执法综合管理信息系统工程方案
- android 磨砂玻璃效果
- notepad++ 插件推荐
- 【1049】晶晶赴约会
- 苹果app取消自动续费_自动续费难取消、广告还得看,你为App会员套路充过多少值...