項目主頁: https://github.com/bang590/JSPatchios
示例下載: https://github.com/ios122/ios122git
JSPatch 可讓你用 JavaScript 書寫原生 iOS APP。只需在項目引入極小的引擎,就可使用 JavaScript 調用任何 Objective-C 的原生接口,得到腳本語言的優點:爲項目動態添加模塊,或替換項目原生代碼動態修復 bug。github
在項目中引入JSPatch,就能夠在發現bug時下發JS腳本替換原生方法,能夠作到無需更新整個APP即時修復bug!安全
JSPatch用iOS內置的 JavaScriptCore.framework做爲引擎;JSPatch也符合蘋果的規則。蘋果不容許動態下發可執行代碼,但經過蘋果 JavaScriptCore.framework 或 WebKit 執行的代碼除外,JS 正是經過 JavaScriptCore.framework 執行的。ruby
JSPatch很是小巧服務器
@implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [JPEngine startEngine]; NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil]; [JPEngine evaluateScript:script]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; [self.window addSubview:[self genView]]; [self.window makeKeyAndVisible]; return YES; } - (UIView *)genView { return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; } @end
// demo.js require('UIView, UIColor, UILabel') defineClass('AppDelegate', { // 替換這個 -genView 方法 genView: function() { var view = self.ORIGgenView(); view.setBackgroundColor(UIColor.greenColor()) var label = UILabel.alloc().initWithFrame(view.frame()); label.setText("JSPatch"); label.setTextAlignment(1); view.addSubview(label); return view; } });
pod 'JSPatch' # 在線更新應用.
複製JSPatch文件夾到你的工程app
導入頭文件#import "JPEngine.h"async
導入本地JS(demo.js)見文首github示例demo(可選,實際項目中,根據本身實際須要進行.)ide
調用[JPEngine startEngine]
加載引擎
經過[JPEngine evaluateScript:@""]
接口執行 JavaScript。
[JPEngine startEngine]; // 直接執行js [JPEngine evaluateScript:@"\ var alertView = require('UIAlertView').alloc().init();\ alertView.setTitle('Alert');\ alertView.setMessage('AlertView from js'); \ alertView.addButtonWithTitle('OK');\ alertView.show(); \ "]; // 從網絡拉回js腳本執行 [NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cnbang.net/test.js"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { NSString *script = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [JPEngine evaluateScript:script]; }]; // 執行本地js文件 NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil]; [JPEngine evaluateScript:script];
// 另外一個例子 // 加載引擎 [JPEngine startEngine]; // 本地JS,動態更新技術就是經過服務器獲取JS更新這個JS NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil]; [JPEngine evaluateScript:script]
// 調用require引入要使用的OC類 require('UIView, UIColor, UISlider, NSIndexPath') // 調用類方法 var redColor = UIColor.redColor(); // 調用實例方法 var view = UIView.alloc().init(); view.setNeedsLayout(); // set proerty view.setBackgroundColor(redColor); // get property var bgColor = view.backgroundColor(); // 多參數方法名用'_'隔開: // OC:NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1]; var indexPath = NSIndexPath.indexPathForRow_inSection(0, 1); // 方法名包含下劃線'_',js用雙下劃線表示 // OC: [JPObject _privateMethod]; JPObject.__privateMethod() // 若是要把 `NSArray` / `NSString` / `NSDictionary` 轉爲對應的 JS 類型,使用 `.toJS()` 接口. var arr = require('NSMutableArray').alloc().init() arr.addObject("JS") jsArr = arr.toJS() console.log(jsArr.push("Patch").join('')) //output: JSPatch // 在JS用字典的方式表示 CGRect / CGSize / CGPoint / NSRange var view = UIView.alloc().initWithFrame({x:20, y:20, width:100, height:100}); var x = view.bounds.x; // block 從 JavaScript 傳入 Objective-C 時,須要寫上每一個參數的類型。 // OC Method: + (void)request:(void(^)(NSString *content, BOOL success))callback require('JPObject').request(block("NSString *, BOOL", function(ctn, succ) { if (succ) log(ctn) })); // GCD dispatch_after(function(1.0, function(){ // do something })) dispatch_async_main(function(){ // do something })
詳細文檔請參考wiki頁面:基礎用法
用 defineClass()
定義 Objective-C 的類,對類和實例方法進行動態替換。
// OC @implementation JPTableViewController ... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSString *content = self.dataSource[[indexPath row]]; //may cause out of bound JPViewController *ctrl = [[JPViewController alloc] initWithContent:content]; [self.navigationController pushViewController:ctrl]; } - (NSArray *)dataSource { return @[@"JSPatch", @"is"]; } - (void)customMethod { NSLog(@"callCustom method") } @end
// JS defineClass("JPTableViewController", { // instance method definitions tableView_didSelectRowAtIndexPath: function(tableView, indexPath) { var row = indexPath.row() if (self.dataSource().count() > row) { //fix the out of bound bug here var content = self.dataSource().objectAtIndex(row); var ctrl = JPViewController.alloc().initWithContent(content); self.navigationController().pushViewController(ctrl); } }, dataSource: function() { // get the original method by adding prefix 'ORIG' var data = self.ORIGdataSource().toJS(); return data.push('Good!'); } }, {})
詳細文檔請參考wiki頁面:defineClass的用法
一些自定義的struct類型、C函數調用以及其餘功能能夠經過擴展實現,調用 +addExtensions:
能夠加載擴展接口:
@implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [JPEngine startEngine]; //添加擴展 [JPEngine addExtensions:@[@"JPInclude", @"JPCGTransform"]]; NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"]; NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil]; [JPEngine evaluateScript:script]; }
include('test.js') //`include()`方法在擴展 JPInclude.m 裏提供 var view = require('UIView').alloc().init() //struct CGAffineTransform 類型在 JPCGTransform.m 裏提供支持 view.setTransform({a:1, b:0, c:0, d:1, tx:0, ty:100})
擴展能夠在JS動態加載,更推薦這種加載方式,在須要用到時才加載:
require('JPEngine').addExtensions(['JPInclude', 'JPCGTransform']) // `include()` and `CGAffineTransform` is avaliable now.
能夠經過新增擴展爲本身項目裏的 struct 類型以及C函數添加支持,詳情請見wiki頁面:添加新擴展
JSPatch很是強大,於是最好將經過服務器獲取JS的連接進行加密,本地JS也最好加密處理
注: 文章由咱們 iOS122 的小夥伴 @偌一茗 整理,喜歡就一塊兒參與: iOS122 任務池