美文网首页iOSIOS-第三方开源库使用IOS
JSPatch (实时修复App Store bug)学习(一)

JSPatch (实时修复App Store bug)学习(一)

作者: wg689 | 来源:发表于2016-04-29 15:39 被阅读1369次

    JSPatch 是一个 iOS 动态更新框架,只需在项目中引入极小的引擎,就可以使用 JavaScript 调用任何 Objective-C 原生接口,获得脚本语言的优势:为项目动态添加模块,或替换项目原生代码动态修复 bug

    基础原理

    JSPatch 能做到通过 JS 调用和改写 OC 方法最根本的原因是 Objective-C 是动态语言,OC 上所有方法的调用/类的生成都通过 Objective-C Runtime 在运行时进行,我们可以通过类名/方法名反射得到相应的类和方法:

    Class class = NSClassFromString("UIViewController");

    id viewController = [[class alloc] init];

    SEL selector = NSSelectorFromString("viewDidLoad");

    [viewController performSelector:selector];

    也可以替换某个类的方法为新的实现:

    static void newViewDidLoad(id slf, SEL sel) {}

    class_replaceMethod(class, selector, newViewDidLoad, @"");

    还可以新注册一个类,为类添加方法:

    Class cls = objc_allocateClassPair(superCls, "JPObject", 0);

    objc_registerClassPair(cls);

    class_addMethod(cls, selector, implement, typedesc);

    对于 Objective-C 对象模型和动态消息发送的原理已有很多文章阐述得很详细,这里就不详细阐述了。理论上你可以在运行时通过类名/方法名调用到任何 OC 方法,替换任何类的实现以及新增任意类。所以 JSPatch 的基本原理就是:JS 传递字符串给 OC,OC 通过 Runtime 接口调用和替换 OC 方法。这是最基础的原理,实际实现过程还有很多怪要打,接下来看看具体是怎样实现的。


    JSPatch github地址 (iOSdemo 下载可以看到效果)

    demo 的实现原理我用下面的一副图来表示,逻辑更清晰,修复其他的bug 原理都差不多,只要你会写JS 代码 理论上是可以修复任何bug 的,还可以动态的为项目添加新的模块

    JSPatch demo的实现原理对比图

    demo 介绍:本来在一个控制器中添加一个btn,btn点击事件没有实现;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //1)开启引擎

    [JPEngine startEngine];

    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];

    //2)加载js脚本(正常情况下是从服务器下载后才使用,达到动态修复app bug 的目的)

    //在这个js 脚本里面使用运行时机制通过类名/方法名调用到任何 OC 方法,替换任何类的实现以及新增任意类,实际上调用了demo 中按钮的点击方法调到一个tableview控制器,demo 中Btn 的点击跳转到tableview的方法没有实现,是app启动的时候加载js,js实现了btn的点击并且跳转到tableview控制器,js到底是如何实现映射是呢?

    NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];

    //执行js脚本

    [JPEngine evaluateScript:script];

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    JPViewController *rootViewController = [[JPViewController alloc] init];

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

    self.window.rootViewController = navigationController;

    [self.window makeKeyAndVisible];

    [[UINavigationBar appearance] setBackgroundImage:nil forBarMetrics:UIBarMetricsCompact];

    return YES;

    }

    JSPatch 实现热更新app 修复线上app的bug 的核心是会写js 脚本下面是js语言实现拦截按钮的点击事件 并且跳转到一个TableView

    defineClass('JPViewController', {  handleBtn: function(sender) {    var tableViewCtrl = JPTableViewController.alloc().init()    self.navigationController().pushViewController_animated(tableViewCtrl, YES)  }})defineClass('JPTableViewController : UITableViewController', ['data'], {

    dataSource: function() {

    var data = self.data();

    if (data) return data;

    var data = [];

    for (var i = 0; i < 20; i ++) {

    data.push("cell from js " + i);

    }

    self.setData(data)

    return data;

    },

    numberOfSectionsInTableView: function(tableView) {

    return 1;

    },

    tableView_numberOfRowsInSection: function(tableView, section) {

    return self.dataSource().length;

    },

    tableView_cellForRowAtIndexPath: function(tableView, indexPath) {

    var cell = tableView.dequeueReusableCellWithIdentifier("cell")

    if (!cell) {

    cell = require('UITableViewCell').alloc().initWithStyle_reuseIdentifier(0, "cell")

    }

    cell.textLabel().setText(self.dataSource()[indexPath.row()])

    return cell

    },

    tableView_heightForRowAtIndexPath: function(tableView, indexPath) {

    return 60

    },

    tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {

    var alertView = require('UIAlertView').alloc().initWithTitle_message_delegate_cancelButtonTitle_otherButtonTitles("Alert",self.dataSource()[indexPath.row()], self, "OK",  null);

    alertView.show()

    },

    alertView_willDismissWithButtonIndex: function(alertView, idx) {

    console.log('click btn ' + alertView.buttonTitleAtIndex(idx).toJS())

    }

    })

    参考文献:

    JSPatch 详解整改版

    ______

    - 作者开发经验总结的文章推荐,持续更新学习心得笔记

    [Runtime 10种用法(没有比这更全的了)](http://www.jianshu.com/p/3182646001d1)

    [成为iOS顶尖高手,你必须来这里(这里有最好的开源项目和文章)](http://www.jianshu.com/p/8dda0caf47ea)

    [iOS逆向Reveal查看任意app 的界面](http://www.jianshu.com/p/060745d5ecc2)

    [JSPatch (实时修复App Store bug)学习(一)](http://www.jianshu.com/p/344db07a2374)

    [iOS 高级工程师是怎么进阶的(补充版20+点)](http://www.jianshu.com/p/1f2907512046)

    [扩大按钮(UIButton)点击范围(随意方向扩展哦)](http://www.jianshu.com/p/ce2d3191224f)

    [最简单的免证书真机调试(原创)](http://www.jianshu.com/p/c724e6282819)

    [通过分析微信app,学学如何使用@2x,@3x图片](http://www.jianshu.com/p/99f1f924ae45)

    [TableView之MVVM与MVC之对比](http://www.jianshu.com/p/d690b5d97201)

    [使用MVVM减少控制器代码实战(减少56%)](http://www.jianshu.com/p/f85363c82ea1)

    [ReactiveCocoa添加cocoapods 配置图文教程及坑总结](http://www.jianshu.com/p/66f0c7e1ced8)

    ______

    - 作者开发经验总结的文章推荐,持续更新学习心得笔记

    [Runtime 10种用法(没有比这更全的了)](http://www.jianshu.com/p/3182646001d1)

    [成为iOS顶尖高手,你必须来这里(这里有最好的开源项目和文章)](http://www.jianshu.com/p/8dda0caf47ea)

    [iOS逆向Reveal查看任意app 的界面](http://www.jianshu.com/p/060745d5ecc2)

    [JSPatch (实时修复App Store bug)学习(一)](http://www.jianshu.com/p/344db07a2374)

    [iOS 高级工程师是怎么进阶的(补充版20+点)](http://www.jianshu.com/p/1f2907512046)

    [扩大按钮(UIButton)点击范围(随意方向扩展哦)](http://www.jianshu.com/p/ce2d3191224f)

    [最简单的免证书真机调试(原创)](http://www.jianshu.com/p/c724e6282819)

    [通过分析微信app,学学如何使用@2x,@3x图片](http://www.jianshu.com/p/99f1f924ae45)

    [TableView之MVVM与MVC之对比](http://www.jianshu.com/p/d690b5d97201)

    [使用MVVM减少控制器代码实战(减少56%)](http://www.jianshu.com/p/f85363c82ea1)

    [ReactiveCocoa添加cocoapods 配置图文教程及坑总结](http://www.jianshu.com/p/66f0c7e1ced8)


    相关文章

      网友评论

      • 马爷:你这个使用的规模大不大?我处在一个 block 编译不过去了
        无畏009:@马爷 block的搞定了么? 我目前也是block这里不会用

      本文标题:JSPatch (实时修复App Store bug)学习(一)

      本文链接:https://www.haomeiwen.com/subject/yftkrttx.html