iOS+JSPatch在线修改app功能

什么是热更新?

举个例子,你的app上架了,但是突然想添加个小功能,那么你有两种方法
  • 第一种方法:在原生代码中修改源代码,然后提交到appStore,这个过程真是很漫长...虽然最近我提交的都是一两天就能得到反馈,但是没人能保证苹果的服务态度一直这样好.有可能10天半个月的也没时间给你审核.我把这个称为冷更新!
  • 第二种方法:就是利用一些三方平台.现在比较火的就是JSPatch之前有(Wax)了. 用官网 的介绍JSPatch 是一个开源项目(Github链接),只需要在项目里引入极小的引擎文件,就可以使用 JavaScript 调用任何 Objective-C 的原生接口,替换任意 Objective-C 原生方法。目前主要用于下发 JS 脚本替换原生 Objective-C 代码,实时修复线上 bug。总之我的认识就是:不用通过重新上架app项目到appstore便可以修改一些小问题!很大程度提高了开发以及维护的效率

JSPatch SDK的接入

JSPatch 基础用法

最后就用JSPatch 这个平台打印一个HelloWorld

  • 本地测试

#import "ViewController.h"

static NSString *identifer = @"cellID";

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
//    NSLog(<#NSString * _Nonnull format, ...#>)
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifer];
        
    }
    
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 4;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"OC--第%ld行", indexPath.row);
}


@end
复制代码
//APPDelete.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   // Override point for customization after application launch.
   
//    [JSPatch startWithAppKey:@"891dfb388fe263a1"];
//    [JSPatch sync];
   
    [JSPatch testScriptInBundle];
   
   return YES;
}
复制代码
  • 然后添加 main.js文件
defineClass("ViewController", {
  tableView_didSelectRowAtIndexPath: function(tableView, indexPath) {
    
  	 console.log("JSPath--:",indexPath.row());

    }
})
复制代码

上面的main.js文件在app启动的时候!会被自动调用:功能就是覆盖ViewController里面的这个方法

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {

   NSLog(@"OC--第%ld行", indexPath.row);
}
复制代码

在调用main.js文件之前

程序的运行结果是这样的

在调用main.js文件之后

程序的运行结果是这样的

到这里大家应该已经看出了这个JSPatch的强大!这还只是本地测试,当你要修改程序内容的时候得在本地修改main.js文件

线上测试 (其实就是把main.js文件传到JSPathch的后台)

然后在APPDelete.m中修改代码

//APPDelete.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   // Override point for customization after application launch.
   //891dfb388fe263a1 这个是你在JSPatch后台创建应用的时候自动生成的appKey
   [JSPatch startWithAppKey:@"891dfb388fe263a1"];
   [JSPatch sync];
   
//     [JSPatch testScriptInBundle];  /** 同时删除本地的main.js文件 */
   
   return YES;
}
复制代码

最后附上github源代码

最后推广一下我的公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值