美文网首页iOS
JSPatch的使用(第一课)

JSPatch的使用(第一课)

作者: voidxin | 来源:发表于2016-07-16 00:18 被阅读110次

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

相关文章

  • JSPatch使用 私有变量操作

    基本使用 ** JSPatch 官方平台 ** ** JSPatch 语法转换 ** JSPatch基本使用 JS...

  • JsPatch学习(1)【使用篇】

    JsPatch的使用很多,简单介绍热修复功能。JsPatch【使用篇】 最简单的使用: 1、注册App 到JSPa...

  • JSPatch的使用(第一课)

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

  • JSPatch基础用法总结

    JSPatch 1.打印console.log(); 2.Protocol 3.Masonry使用JSPatch如...

  • 网站

    iOS常用 XCode代码混淆 Mac常用软件破解版下载 mongodb jspatch介绍 jspatch使用 ...

  • JSPatch 使用

    JSPatch以小巧的体积做到了让JS调用/替换任意OC方法,让iOS APP具备热更新的能力,在实现 JSPat...

  • JSPatch的使用

    1, JSPatch热更新 众所周知,AppStore 上发布需要有个一非常恶心的审核期,而且很可能被拒绝掉,发布...

  • JSPatch的使用

    1.下载地址 在JSpatch的官网注册账号。之后在首页的右上角点击 我的APP 。之后,按要求填入APP的名字和...

  • JSPatch的使用

    JSPatch 是一个 iOS 动态更新框架,只需在项目中引入极小的引擎,就可以使用 JavaScript 调用任...

  • 使用七牛下发JSPatch文件及文件加密

    使用JSPatch这么久了,一直都是使用http://jspatch.com/平台集成SDK来实现js文件下发和版...

网友评论

    本文标题:JSPatch的使用(第一课)

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