zoukankan      html  css  js  c++  java
  • 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 还是可以用的。

         

  • 相关阅读:
    用函数装饰一首诗
    [转]最常用的15大Eclipse开发快捷键技巧
    [转]python 模块 chardet下载及介绍
    python手动设置递归调用深度
    view-xpath
    开源项目的贡献流程
    MIT许可证
    scrapy分布式的几个重点问题
    【bzoj1026】[SCOI2009]windy数 数位dp
    【bzoj5064】B-number 数位dp
  • 原文地址:https://www.cnblogs.com/gulong/p/5407980.html
Copyright © 2011-2022 走看看