JSPatch的使用(第一课)

JSpatch的更多用法可以去github上找文档。不多解释。
先来个简单的demo:
1:先百度“JSPatch”,进入如下页面:


首页@2x.png

2:注册账号,添加APP项目:

创建app.png

3:获得appkey
4: 下载 SDK 并导入项目
下载 SDK 后解压,将JSPatch.framework
拖入项目中,勾选 "Copy items if needed",并确保 "Add to target" 勾选了相应的 target。


SDK1

5:在appdelegate中加入以下代码:

#import <JSPatch/JSPatch.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [JSPatch startWithAppKey:@"你的AppKey"];
    [JSPatch sync];
    ...
}
@end

6:在viewController中添加一个TableVIew,写入以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self addUI];
   // [self dataBegin];
}

-(void)addUI{
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
}
#pragma mark -tableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indefier=@"CELL";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indefier];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indefier];
    }
    cell.textLabel.text=[NSString stringWithFormat:@"%ld",indexPath.row];
    
    return cell;
}

#pragma mark tableView delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

#pragma mark -getter
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate=self;
        _tableView.dataSource=self;
    }
    return _tableView;
}

运行程序,控制台输出:

2016-07-16 00:30:06.074 JSPatchHotFixDemo[3309:137704] JSPatch: runScript
2016-07-16 00:30:06.086 JSPatchHotFixDemo[3309:137704] JSPatch: evaluated script, length: 992
2016-07-16 00:30:06.087 JSPatchHotFixDemo[3309:137704] JSPatch: request http://q.jspatch.com/a368c7abdb625542/1.0.0?v=1468600206.087138
2016-07-16 00:30:06.296 JSPatchHotFixDemo[3309:137899] JSPatch: request success {
    v = 2;
}

表明集成JSPatch成功。
运行app,我们发现页面是这样的:

QQ20160715-1@2x.png

接下来我们希望重写-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法改变cell的文字,然后增加cell的高度。
7:创建main.js文件,在文件中写入以下代码:

require('VXJspatchTestViewController');
 require('UIColor');
defineClass("ViewController", {
            
            tableView_cellForRowAtIndexPath: function(tableView, indexPath) {
            var cell = tableView.dequeueReusableCellWithIdentifier("cell")
            if (!cell) {
            cell = require('UITableViewCell').alloc().initWithStyle_reuseIdentifier(0, "cell")
            }
            cell.textLabel().setText("去吧皮卡丘");
            return cell
            },
            tableView_heightForRowAtIndexPath: function(tableView, indexPath) {
                 return 100;
            },
            //instance method definitions
            tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
            var testVC = VXJspatchTestViewController.alloc().init();
            testVC.setTitle("我是JSPatch增加的方法");
            self.navigationController().pushViewController_animated(testVC, YES);
           
            }
            }, {});

如果你不会写JSPatch代码也没关系,百度“JSPatch Conventor",写入OC源码,自动翻译。
8:创建版本号,上传main.js文件:


QQ20160715-2@2x.png

9:ok,发布成功,退出app,重新启动运行,恭喜你,实现动态更新,tableViewcell的两个datasource和一个delegate方法已被重写(如果你发现你的代码没起作用,请退出重新启动多次):


QQ20160716-0@2x.png

点击cell,push到如下页面:
QQ20160716-1@2x.png

自此,我们实现了一个简单的动态更新,如果以后你的项目想在发版之后修复bug的话,目前使用JSpatch是比较流行的选择。
无Demo不文章,欢迎访问我的github下载本文的Demo:https://github.com/voidxin/JSPatchHotFixDemo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • Swift版本点击这里欢迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh阅读 25,569评论 7 249
  • 情绪的力量 2015-2016赛季的NBA,注定将载入史册。 常规赛,金州勇士队打出了史诗一般的一个赛季,拿到了前...
    枷行栅言阅读 164评论 0 0
  • 学习了1.理想中的房间是像日本的那样,床铺晚上拿出来,白天收起床铺,铺个垫子,喝茶看书,或是瑜伽锻炼。无须厚重的木...
    江狒狒阅读 185评论 0 1
  • day 8《阿弥陀佛么么哒》 不管是欠别人 还是欠自己 你曾欠下过多少个 对不起 时间无情第一 它才不在乎你...
    Molly_zhang阅读 117评论 0 0