一说到热修复,可能不少人会以为应该很复杂,很难用(我之前是这么以为的。。。),实际使用起来蛮简单的,这里以一个小demo演示热修复是如何修复崩溃的,具体更深刻的用法,能够看这个
https://github.com/bang590/JSPatch/wiki/JSPatch-%E5%9F%BA%E7%A1%80%E7%94%A8%E6%B3%95git
实现原理:https://github.com/bang590/JSPatch/wiki/JSPatch-实现原理详解github
打开JSPatch网站,下载SDK:http://jspatch.com/Index/sdk并发
新建一个项目,名为JSPatchDemo,将下载后的JavaScriptCore.framework文件拖到项目中,并导入libz.dylib(或libz.tbd) 和 JavaScriptCore.frameworkapp
在AppDelegate里配置,startWithAppKey须要配上本身的Key,在第三步会详细介绍。jsp
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [JSPatch startWithAppKey:@"你的APPKey"]; //用来检测回调的状态,是更新或者是执行脚本之类的,相关信息,会打印在你的控制台 [JSPatch setupCallback:^(JPCallbackType type, NSDictionary *data, NSError *error) { }]; [JSPatch setupDevelopment]; [JSPatch sync]; return YES; }
在ViewController里写上一个方法为jsPatchTest,用于改变文本的文字。ide
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UILabel *label; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _label = [[UILabel alloc] init]; _label.font = [UIFont systemFontOfSize:14]; _label.frame = CGRectMake(50, 100, 150, 50); _label.backgroundColor = [UIColor yellowColor]; [self.view addSubview:_label]; [self jsPatchTest]; } - (void)jsPatchTest { self.label.text = @"哈哈哈哈哈哈"; } @end
打开JSPatch官网点击左上角注册 -> http://www.jspatch.com/工具
点新增APP,随便填写APP名,如:网站
将AppKey填写到AppDelegate--StartWithAppKey中atom
点击添加版本,填写和工程目录的一致,如1.0加密
建立一个main.js文件并在里面写上如下代码
defineClass('ViewController', { jsPatchTest : function() { self.label().setText("label的text被改掉了"); }, })
发布补丁
再次从新打开app,你会发现,会报http的错。。。在info.plist里加上以下代码,容许http访问
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
整体使用仍是比较简单的,更多功能能够去文档中发掘
一、可能不少人都不会写js补丁,好在JSPatch做者还为咱们准备了另外一个工具。
http://bang590.github.io/JSPatchConvertor/
这个工具能够帮助咱们转换OC代码为JS
二、JS必定要加密,下面是方法截图和文档: