JSPatch热更新的利器.

时间:2023-03-09 17:58:48
JSPatch热更新的利器.

如果用一句话来描述JSPatch,就是利用系统自带的JavaScriptCore.framework配合RunTime机制,进行实时的代码下载与运行..

而且使用也很简单,启动,加载JS,运行...

    [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];

关键是装载进JSPatch的js怎么写.

require('UIAlertView') //需要引入的类
defineClass('LifeViewController', { //作用于那个类
viewWillAppear: function(animated) { //重写那个函数
self.super().viewWillAppear(animated);
var alert = UIAlertView.alloc().initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles("Hot - Fix Test", "热更新测试Hot-Fix Test", self, "取消", "确定", null);
alert.setTag(1);
alert.show();
},
});

不过也提供了将oc代码直接转成js的工具.

http://bang590.github.io/JSPatchConvertor/

其实JSPatch的想象空间很大. 如果运动得当,灵活性将不输于H5,但体验绝对秒杀H5.虽然一般拿来修复Bug...