1.JSPatch 準備php
地址:https://github.com/bang590/JSPatchgit
框架:libz.1.tbd , JavaScriptCore.frameworkgithub
2.cocospod(可獲得的三個文件)app
JPEngine.h,JPEngine.m,JSPatch.js框架
3.Demo(重要的三個文件夾)jsp
Extensions,JSPatch,Loaderide
********測試********測試
1.在本地生成一個main.js文件ui
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 仍是能夠用的。