JSPath使用实例

之前一直就听闻JSPath的强大,是上线热更新的好帮手,不过一直没去窥探,今日正好闲来无事,撸了个小Demo,着实让我吃惊,实在太强大了,不仅可以用来修复Bug,同时也可以增加功能,反正能执行你的代码,想怎么玩就看你需求和想象了。

创建JSPath账号

  • 进入JSPath官网
  • 注册账号
  • 添加App,并且拷贝appkey

在项目中集成JSPath

  • 集成方法很简单,按照官网操作
  • 设置Appkey,代码如下
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    [JSPatch startWithAppKey:@"eff82c1659bf1976"];
    [JSPatch sync];
    return YES;
    }

原来有bug的代码

demo很简单,一个UINavigationController装这一个UITableViewController

#import "ViewController.h"

@interface ViewController ()
{
    NSArray *_dataSource;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*这里让线程睡眠2秒,因为jspath是通过网络下发代码,
     *这个工程一启动就执行当前代码,但是js文件还没下载下来,就直接崩溃了*/
    [NSThread sleepForTimeInterval:2];
    
    _dataSource=@[@"1⃣️",@"2⃣️",@"3⃣️",@"4⃣️",@"🈚️",@"6⃣️",];
}

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

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId=@"cellId";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellId];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    
    //这里会报错,我的素组只有6个元素,但是uitableview有10行
    cell.textLabel.text=_dataSource[indexPath.row];
    return cell;
}

@end

现在运行立马崩溃,因为数组越界了


1.png

使用JSPath热修复

  • 编写js代码,并且命名为main.js(官方要求),JSPath的语法看官方文档,js代码如下
defineClass("ViewController", {
tableView_cellForRowAtIndexPath: function(tableView, indexPath) {
    var cellId='cellId';
    var cell=tableView.dequeueReusableCellWithIdentifier(cellId);
    if(cell==0)
    {
        cell=require('UITableViewCell').alloc().initWithStyle_reuseIdentifier(0,cellId);
    }
    var row = indexPath.row()
    var datasource=self.valueForKey('_dataSource');
    if (datasource.count() > row) {  //加上判断越界的逻辑
        cell.textLabel().setText(datasource.objectAtIndex(row));
    }
    return cell;
}
            
}
)

defineClass("ViewController", {
            tableView_didSelectRowAtIndexPath: function(tableView, indexPath){
            //声明类,使用OC类都需要先声明才可以使用
            require('UIViewController,UIColor');
            //创建控制器
            var vc=UIViewController.alloc().init();
            vc.setTitle("JSPath");
            vc.view().setBackgroundColor(UIColor.whiteColor());

            //跳转新的控制器
            self.navigationController().pushViewController_animated(vc,1);

            //打印日志
            console.log(indexPath);
            }
})```
是的,我不仅修复了数组越界崩溃的代码,还画蛇添足加来点击cell Push一个控制器的功能。

- 上传js代码
 - 登录JSPath
 - 进入我的“我的App”页面
 - 选择添加的app,点击【管理】
 - 点击【添加App版本】。(注意:版本号必须和线上app版本号一致,否则没用)
 - 上传js文件

###大功告成
再次运行项目,不需要改任何代码,就可以达到期望的要求了
![cIzd5d0H4Z.gif](http://upload-images.jianshu.io/upload_images/433584-c4ed250f739b9901.gif?imageMogr2/auto-orient/strip)

###调试模式
在调试的时候,可以把main.js直接拖到项目,并且在appdelegate 配置JSPath调试模式,这样的话,就不会去线上请求代码,而是直接运行你本地的js文件。修改后的代码如下
  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    // [JSPatch startWithAppKey:@"eff82c1659bf1976"];
    // [JSPatch sync];
    [JSPatch testScriptInBundle]; //开启调试模式
    return YES;
    }

### 题外话
到了这里,你是不是觉得挺震惊,同时也感觉挺危险,在后台几句JS代码就可以控制器你的线上App,万一js文件被拦截了呢?那岂不是为所欲为啦?
不过不用担心,JSPath为我们提供了加密方式,在上次js文件的时候可以选择“使用RSA Key” 进行加密。总体来说,我觉得这个技术还是挺不错的,值得学习,值得使用。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容