Last active
July 3, 2020 05:16
-
-
Save Skifary/f0d36bd5e9073b6b598eecf78d533d54 to your computer and use it in GitHub Desktop.
替换block原有实现
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
typedef struct __block_impl { | |
void *isa; | |
int Flags; | |
int Reserved; | |
void *FuncPtr; | |
}__block_impl; | |
typedef void(^GlobalBlock)(void); | |
void hookFunc() { | |
NSLog(@"hello world"); | |
} | |
void HookBlockToPrintHelloWorld(id blk) { | |
__block_impl *ptr = (__bridge __block_impl *)blk; | |
ptr->FuncPtr = &hookFunc; | |
} | |
int main(int argc, const char * argv[]) { | |
GlobalBlock blk = ^void() { | |
}; | |
HookBlockToPrintHelloWorld(blk); | |
blk(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
原理:参照block内部结构,修改其函数指针即可改变任意block的函数部分