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