iOS+JSPatch在线修改app功能-b

什么是热更新?

举个例子,你的app上架了,可是忽然想添加个小功能,那么你有两种方法
  • 第一种方法:在原生代码中修改源代码,而后提交到appStore,这个过程真是很漫长...虽然最近我提交的都是一两天就能获得反馈,可是没人能保证苹果的服务态度一直这样好.有可能10天半个月的也没时间给你审核.我把这个称为冷更新!git

  • 第二种方法:就是利用一些三方平台.如今比较火的就是JSPatch以前有(Wax)了. 用官网 的介绍JSPatch 是一个开源项目(Github连接),只须要在项目里引入极小的引擎文件,就可使用 JavaScript 调用任何 Objective-C 的原生接口,替换任意 Objective-C 原生方法。目前主要用于下发 JS 脚本替换原生 Objective-C 代码,实时修复线上 bug。总之个人认识就是:不用经过从新上架app项目到appstore即可以修改一些小问题!很大程度提升了开发以及维护的效率github

JSPatch SDK的接入

JSPatch 基础用法

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

  • 本地测试app

#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文件ide

    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以后.gifatom

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

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

线上发布新补丁.pngspa

而后在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.pngcode

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

相关文章
相关标签/搜索