zoukankan      html  css  js  c++  java
  • iOS+JSPatch在线修改app功能-b

    什么是热更新?

    举个例子,你的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文件之前

    程序的运行结果是这样的

    使用JSPatch之前.gif

    在调用main.js文件之后

    程序的运行结果是这样的

    使用JSPatch之后.gif

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

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

    线上发布新补丁.png

    然后在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;
    }

    线上JSPathch.png

    最后附上github源代码:https://github.com/yuzhouheike/JSPathch-

  • 相关阅读:
    eslint 的 env 配置是干嘛使的?
    cookie httpOnly 打勾
    如何定制 antd 的样式(theme)
    剑指 Offer 66. 构建乘积数组
    剑指 Offer 65. 不用加减乘除做加法
    剑指 Offer 62. 圆圈中最后剩下的数字
    剑指 Offer 61. 扑克牌中的顺子
    剑指 Offer 59
    剑指 Offer 58
    剑指 Offer 58
  • 原文地址:https://www.cnblogs.com/isItOk/p/5755081.html
Copyright © 2011-2022 走看看