JSPatch 使用

1.JSPatch 准备

    地址:https://github.com/bang590/JSPatch

    框架:libz.1.tbd , JavaScriptCore.framework

2.cocospod(可得到的三个文件)

   JPEngine.h,JPEngine.m,JSPatch.js

3.Demo(重要的三个文件夹)

   Extensions,JSPatch,Loader

   

 

********测试********

1.在本地生成一个main.js文件

require('SecondViewController')
defineClass('ViewController',{
            rightClick:function(sender){
            var vc = SecondViewController.alloc().init()
            self.navigationController().pushViewController_animated(vc,YES)
            }
            })

2.JSPatchDemo

2.1 ViewController

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"right" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightClick:)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
//    [SecondViewController setGlBlock:^(NSString *) {
//        
//    }];
}

#pragma mark - click
- (IBAction)rightClick:(id)sender{
    NSLog(@"某某某");
}

@end

2.2 AppDelegate (执行main.js)

  2.2.1 使用JPEngine (点击ViewController的rightNavigationItem 会跳转SecondViewController)      

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [JPEngine startEngine];
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"main" ofType:@"js"];
    NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:NULL];
    [JPEngine evaluateScript:script];
    return YES;
}

  2.2.2 使用JPLoader(5s前点击ViewController的rightNavigationItem 会打印'某某某',5s后会跳转SecondViewController)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [JPLoader runTestScriptInBundle];  //5.0秒之前执行 本来就有的方法,5.0秒之后执行js里面的方法
        [JPLoader setLogger:^(NSString *log) {
            NSLog(@"hgl say:\"%@\"",log);
        }];
    });
    
    return YES;
}

 

 

*********线上***********

1.服务端

  文件管理 baseURL/version/v2.zip       (version:app版本号,v2:js版本号)

  接口需求(request: {version:当前版本号,index:js版本号(为空是表示未下载过js.main)},

              response:{state:状态码,update:是否需要下载,operate:是否需要执行当前js})

2.打包main.js

   $php pack.php main.js -o v2 (pack.php 存在于Loader文件夹中,pack.php需要RSA私钥)

   使用RSA加密验证 (只是为了验证,无法避免转包。如若需要可使用https协议)

    *生成RSA公钥私钥

    1.安装openssl

      sudo apt-get install openssl
    2.生成RSA私钥 //该命令会生成1024位的私钥
      openssl genrsa -out rsa_private_key.pem 1024
    3.把RSA私钥转换成PKCS8格式
      openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM –nocrypt
    4.生成公钥
      openssl rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout

 3.客户端

    3.1 请示是否需要下载js,是否需要执行js

    3.2 下载main.js

[JPLoader updateToVersion:3 callback:^(NSError *error) {
        
    }];

   3.3 下载完成之后执行main.js

[JPLoader updateToVersion:3 callback:^(NSError *error) {
        [JPLoader run];
    }];

     *注:1.JPLoader中需要放入RSA的公钥

        2. updateToVersion方法会下载zip包,zip解压-> RSA验证 ->成功 -> 回调

        3. JSPatch平台托管 http://jspatch.com/Apps/index

                   4. 阿里百川平台托管 http://baichuan.taobao.com

 

 

 

注:2017-03 苹果官网 已经不允许app 动态修改一些东西,上App Store的应用最好先别使用这个东西。企业版app 还是可以用的。

     

posted @ 2016-04-19 16:49  古龙•历山大亚  阅读(768)  评论(0编辑  收藏  举报