oc@swift|oc@swift hook
2021-01-01 00:00:00
cxdt
oc
///////////////////////////
#import#import "Hook.h"
@implementation Hook
+ (void)sMethod:(SEL)ag1 sClass:(Class)class wMethod:(SEL)ag2 {
Method oMethod = class_getInstanceMethod(class, ag1);
Method nMethod = class_getInstanceMethod(class, ag2);
method_exchangeImplementations(oMethod, nMethod);
}
@end
#import "ViewController.h"
#import "Hook.h"
@interface ViewController ()
@end
@implementation ViewController
+ (void)load{
[Hook sMethod:@selector(click:) sClass:[self class] wMethod:@selector(hookClick:)];
}
- (void)hookClick:(id)sender {
NSLog(@"hook hahhaha");
[self hookClick:sender];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)click:(id)sender {
NSLog(@"hahahha");
}
@end
swift ////////////////////////////////////////// 类似
class Hook: NSObject {
class func sMethod(ag1: Selector,sClass: AnyClass, ag2: Selector) {
let oMethod = class_getInstanceMethod(sClass, ag1)
let nMethod = class_getInstanceMethod(sClass, ag2)
method_exchangeImplementations(oMethod, nMethod)
}
}
import UIKit
class ViewController: UIViewController {
override func loadView() {
super.loadView()
Hook.sMethod(ag1: #selector(click(_:)),sClass: ViewController.self , ag2: #selector(hookClick(_:)))
}
@objc func hookClick(_ sender: UIButton) {
print("hook HAHA")
click(sender)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func click(_ sender: UIButton) {
print("hahaha")
print("-------------")
}
【oc@swift|oc@swift hook】}
推荐阅读
- 有手就行7——*项目构建细节2-钩子(webhook)|有手就行7——*项目构建细节2-钩子(webhook) 配置
- Android免Root|Android免Root 修改程序运行时内存指令逻辑(Hook系统函数)
- React|React Hooks 入门记录
- 2021-01-01晨间日记
- iOS|iOS UI控件埋点技术方案之基于runtime hook
- 根据tapable调试结果,手写SyncHook实现
- 谈谈React|谈谈React Hooks 与Vue3.0 Function based API的区别()
- React-Hooks设计动机和工作模式
- FridaHook环境搭建
- iOS 逆向之hook系统app