JSPatch的使用(第一课)

本文详细介绍了如何使用JSPatch实现iOS应用的热更新功能,包括注册账号、添加项目、集成SDK、重写TableView数据源及代理方法等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值