當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JSPatch
JSPatch
原理:在APP啟動的時(shí)候,通過JavaScrptCore來執(zhí)行編寫的JavaScript腳本,利用OC的運(yùn)行時(shí)特性來修改類的方法和屬性。
1:執(zhí)行JS腳本
#import "AppDelegate.h" // 導(dǎo)入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 修復(fù)文件下載失敗"); #endif}];self.window = [[UIWindow alloc] initWithFrame:MainScreenBounds];s self.window.rootViewController = [self FF_GetTabBarController]; [self.window makeKeyAndVisible];return YES; }
2: 在動態(tài)修改OC類的方法和屬性時(shí)的核心方法 defineClass('className', instanceMethod, classMethod)
?? className--要覆蓋的類名,字符串類型
?? instanceMethod--實(shí)例方法
?? classMethod--類方法(要修改類方法,必須有實(shí)例方法,如果實(shí)例方法省略,就會默認(rèn)為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: 使用_鏈接多個(gè)參數(shù)2.Use __ to represent _ in the Objective-C method name: 如果原來的方法名中有_用__代替3.Call original method by adding prefix ORIG to the method name. 如果想調(diào)用OC中對應(yīng)的初始方法,添加前綴ORIG4.Super / Property / Member variablesa: Use self.super() to call super methods. 使用self.super()調(diào)用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 一個(gè)新屬性// 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.在已經(jīng)存在的類中添加一個(gè)新方法,在OC中回調(diào)是,參數(shù)是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導(dǎo)入 require('UIView, NSString, NSDate, NSLog') /// 要重載的類, 修改viewDidLoad方法 defineClass('FFBaseViewController',{/// 實(shí)例方法viewDidLoad: function(){/// self.super() 回調(diào) super method self.super().viewDidLoad();/// 要執(zhí)行的操作 ...};})/// 要重載的類, 調(diào)用viewDidLoad方法 defineClass('FFBaseViewController',{/// 實(shí)例方法viewDidLoad: function(){/// self.super() 回調(diào) parent method self.ORIGviewDidLoad();/// 要執(zhí)行的操作 ...};})/// 要重載的類, 修改viewDidLoad方法, 添加一個(gè)類方法FFPrint defineClass('FFBaseViewController',{/// 實(shí)例方法viewDidLoad: function(){/// self.super() 回調(diào) parent method self.super().viewDidLoad();/// 要執(zhí)行的操作 ...};},{/// 類方法 FFPrint: functon(){NSLog('類方法');}} )?
??
?
轉(zhuǎn)載于:https://www.cnblogs.com/jisa/p/10792483.html
總結(jié)
- 上一篇: jQuery实现模拟淘宝精品
- 下一篇: mac book a1278