日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

JSPatch

發布時間:2023/12/29 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JSPatch 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JSPatch

原理:在APP啟動的時候,通過JavaScrptCore來執行編寫的JavaScript腳本,利用OC的運行時特性來修改類的方法和屬性。

1:執行JS腳本

#import "AppDelegate.h" // 導入JSPatch框架 #import <JSPatch/JPEngine.h>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[Bugly startWithAppId:BuglyAppId];[JPEngine startEngine]; // 加載本地的JS文件 // NSString *path = [[NSBundle mainBundle] pathForResource:@"firstChange" ofType:@"js"]; // NSString *script = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; // [JPEngine evaluateScript:script];// 記載請求的JS文件[[FFNetworkHelper share] GetFile:[NSString stringWithFormat:@"xxx/%@", @"firstChange.js"] successBlock:^(id _Nonnull responseObject) {[JPEngine evaluateScript:responseObject];} failuerBlock:^(NSString * _Nonnull str) { #if DEBUGFFShowInfo(@"JS 修復文件下載失敗"); #endif}];self.window = [[UIWindow alloc] initWithFrame:MainScreenBounds];
s self.window.rootViewController = [self FF_GetTabBarController]; [self.window makeKeyAndVisible];return YES; }

2: 在動態修改OC類的方法和屬性時的核心方法 defineClass('className', instanceMethod, classMethod)

?? className--要覆蓋的類名,字符串類型

?? instanceMethod--實例方法

?? classMethod--類方法(要修改類方法,必須有實例方法,如果實例方法省略,就會默認為instanceMethod)

/* ##APIdefineClass(classDeclaration, instanceMethods, classMethods)@param classDeclaration: class name, super classname and protocols @param instanceMethods: instance methods you want to override or add @param classMethods: class methods you want to override or add1.Use _ to separate multiple params: 使用_鏈接多個參數2.Use __ to represent _ in the Objective-C method name: 如果原來的方法名中有_用__代替3.Call original method by adding prefix ORIG to the method name. 如果想調用OC中對應的初始方法,添加前綴ORIG4.Super / Property / Member variablesa: Use self.super() to call super methods. 使用self.super()調用super methodb: Call methods to get / set existing property. 通過get / set 方法來操作存在的屬性// JSdefineClass("JPTableViewController", {viewDidLoad: function() {var data = self.data() //get property valueself.setData(data.toJS().push("JSPatch")) //set property value},})c: Use getProp() and setProp_forKey() to get / set new property 通過getProp() 和 setProp_forKey()來get / set 一個新屬性// JSdefineClass("JPTableViewController", {init: function() {self = self.super().init()self.setProp_forKey("JSPatch", "data") //add new property (id data)return self;}viewDidLoad: function() {var data = self.getProp("data") //get the new property value},})d: Use valueForKey() and setValue_forKey() to get / set member variables 成員變量操作// JSdefineClass("JPTableViewController", {viewDidLoad: function() {var data = self.valueForKey("_data") //get member variablesself.setValue_forKey(["JSPatch"], "_data") //set member variables},}) 5.You can add new methods to the existing Class, if you want to call the new method in Objective-C, all the params type is id.在已經存在的類中添加一個新方法,在OC中回調是,參數是id類型6.Special typesUse hash object to represent CGRect / CGPoint / CGSize / NSRange// JSvar view = UIView.alloc().initWithFrame({x:20, y:20, width:100, height:100})view.setCenter({x: 10, y: 10})view.sizeThatFits({width: 100, height:100})var x = view.frame.xvar range = {location: 0, length: 1}*//// 要用到的類,用require導入 require('UIView, NSString, NSDate, NSLog') /// 要重載的類, 修改viewDidLoad方法 defineClass('FFBaseViewController',{/// 實例方法viewDidLoad: function(){/// self.super() 回調 super method self.super().viewDidLoad();/// 要執行的操作 ...};})/// 要重載的類, 調用viewDidLoad方法 defineClass('FFBaseViewController',{/// 實例方法viewDidLoad: function(){/// self.super() 回調 parent method self.ORIGviewDidLoad();/// 要執行的操作 ...};})/// 要重載的類, 修改viewDidLoad方法, 添加一個類方法FFPrint defineClass('FFBaseViewController',{/// 實例方法viewDidLoad: function(){/// self.super() 回調 parent method self.super().viewDidLoad();/// 要執行的操作 ...};},{/// 類方法 FFPrint: functon(){NSLog('類方法');}} )

?

??

?

轉載于:https://www.cnblogs.com/jisa/p/10792483.html

總結

以上是生活随笔為你收集整理的JSPatch的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。