A solution for centralized method redirection.
You can read articles about underlying implementation of this project.
The interface of TrampolineHook is very very simple. Only two steps are required.
- Create the global interceptor with your centralized function as follows:
// Suppose you have function defined as
// C Format
void myInterceptor
{
// bla bla bla
}
// Create the global inteceptor with your function
THInterceptor *interceptor = [THInterceptor sharedInterceptorWithFunction:(IMP)myInterceptor]
- Intercept any function you want no matter what the method signature it is.
// Suppose you want to intercept the call of - [UIView initWithFrame:]
Method m = class_getInstanceMethod([UIView class], @selector(initWithFrame:));
IMP imp = method_getImplementation(m);
// Intercept the imp
THInterceptorResult *result = [interceptor interceptFunction:imp];
// You can check the result.state to find whether the inteception is successfully carried out or not.
result.state == THInterceptStateSuccess
The debug of interception is not very easy.
Remember always use the result.replacedAddress
returned from interceptFunction
for breakpoint.
There is one typical example associated with this open source project called MainThreadChecker。It is the rewritten version of the implementation in Apple libMainThreadChecker.dylib
It is almost the same as the one of Apple based on my own reverse engineering. No Private APIs used.
The usage of MainThreadChecker is quite easy.
+ (void)load
{
[MTInitializer enableMainThreadChecker]
}
- API Stability.
- Varadic Argument Interceptor.
- More examples.
- Performance Benchmark.
The great works listed below inspired me a lot during the development of TrampolineHook.
- Dobby @jmpews
Copyright 2020 @SatanWoo
Checkout License