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...

posted @ 2016-06-28 22:21  NGI.  阅读(535)  评论(0编辑  收藏  举报
GitHub | M1989