国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSPatch庫,一個Apple官方支持的實現在線更新iOS應用的庫

2019-11-14 17:57:54
字體:
來源:轉載
供稿:網友

簡介

JSPatch 可以讓你用 javaScript 書寫原生 iOS APP。只需在項目引入極小的引擎,就可以使用 Javascript 調用任何 Objective-C 的原生接口,獲得腳本語言的優勢:為項目動態添加模塊,或替換項目原生代碼動態修復 bug。

優勢

  • 在項目中引入JSPatch,就可以在發現bug時下發JS腳本替換原生方法,可以做到無需更新整個APP即時修復bug!

  • JSPatch用iOS內置的 JavaScriptCore.framework作為引擎;JSPatch也符合蘋果的規則。蘋果不允許動態下發可執行代碼,但通過蘋果 JavaScriptCore.framework 或 WebKit 執行的代碼除外,JS 正是通過 JavaScriptCore.framework 執行的。

  • 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.jsrequire('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;  }});

安裝

通過Cocopods安裝

pod 'JSPatch' # 在線更新應用.

手動導入

  1. 下載https://github.com/bang590/JSPatch并解壓

  2. 復制JSPatch文件夾到你的工程

使用

objective-C:

  1. 導入頭文件#import "JPEngine.h"

  2. 導入本地JS(demo.js)見文首github示例demo(可選,實際項目中,根據自己實際需要進行.)

  3. 調用[JPEngine startEngine] 加載引擎
  4. 通過[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更新這個JSNSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"js"];NSString *script = [NSString stringWithContentsOfFile:sourcePath encoding:NSUTF8StringEncoding error:nil];[JPEngine evaluateScript:script]

JavaScript:

基礎使用方式

// 調用require引入要使用的OC類require('UIView, UIColor, UISlider, NSIndexPath')// 調用類方法var redColor = UIColor.redColor();// 調用實例方法var view = UIView.alloc().init();view.setNeedsLayout();// set PRoertyview.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 / NSRangevar 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))callbackrequire('JPObject').request(block("NSString *, BOOL", function(ctn, succ) {  if (succ) log(ctn)}));// GCDdispatch_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
// JSdefineClass("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 任務池


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天等县| 高阳县| 德格县| 嵊泗县| 横山县| 毕节市| 辽源市| 浮山县| 嘉禾县| 邵阳县| 修武县| 石楼县| 万州区| 靖江市| 将乐县| 石城县| 英山县| 大英县| 塔河县| 汉沽区| 石渠县| 双城市| 寿阳县| 宁武县| 雷山县| 尚义县| 荣昌县| 盈江县| 辽阳市| 阜宁县| 勃利县| 张家口市| 舟山市| 山西省| 上杭县| 乌兰浩特市| 阿拉善右旗| 长治市| 灵宝市| 武隆县| 玉门市|