Method Swizzling (Methods leveling)
How to say, first understand what is the hook why hook, learn C + + friends should be clear, hook is used to obtain (truncate/change) The method of the underlying call. So that we can freely modify or read some of the things we want. (Personal understanding)
Here is the explanation of Baidu Encyclopedia: Hook, is a Windows message processing mechanism of a platform, the application can set up a child to monitor a specific window of a message, and the window can be monitored by other processes created. When the message arrives, it is processed before the target window handles the function. Hook mechanism allows applications to intercept processing of window messages or specific events
That iOS we use Method swizzling to achieve, why is the internal hook, because it needs to be implemented in the project, I will share the external.
----------------------------------------------Messy split-line------------------------------------------
First look at the SEL and IMP concepts,
SEL can be understood as the meaning of the function name, and our common @selector () is to get the SEL through a string
IMP can be understood as a function pointer meaning that it is able to read the contents of the function correctly
This is usually the case: stealing a picture
All we have to do is untie the link and connect it to our custom function imp, and if necessary, we'll connect back to the original imp.
This is the case:
If you want to continue calling IMPC after you finish executing IMPN, you only need to call Selectorn in IMPN.
---------------------------------------------Messy split-line----------------------------------------
How to do it specifically:
Method Origmethod = Class_getinstancemethod (class, Origselector); Get the Sel method
method is a struct, we want the imp to be inside and look at the structure
struct objc_method { SEL method_name OBJC2_UNAVAILABLE; char *method_types OBJC2_UNAVAILABLE; IMP method_imp objc2_unavailable;}
IMP origimp = method_getimplementation (Origmethod); Get the Imp in method
Ok,imp get to, connect sel to other imp?
BOOL Class_addmethod (class cls, SEL name, IMP imp, const char *types); First add the new method name sel+ original Impimp method_setimplementation (method M, imp imp); The original method (SEL) is then reassigned to the new IMP
void Method_exchangeimplementations (Method m1, method m2)//or the method of exchange can be used
---------------------------------------------Messy split-line-------------------------------------
In actual combat, suppose we want to know what value the app jumps send (such as app call QQ share what), then we can check UIApplication OpenURL method
#import "KHookObjectWrapper.h" #import " Uikit/uikit.h "#import <objc/objc.h> #import <objc/runtime.h> @implementation khookobjectwrapper+ (void) setup{ //openurl method m = class_getinstancemethod ([uiapplication class], @selector (OpenURL:)); class_addmethod ([uiapplication class], @selector (hook_openurl:), method_getimplementation (m), method_gettypeencoding (m)); method_setimplementation (M, class_ Getmethodimplementation ([self class], @selector (hook_openurl:)));} - (BOOL) Hook_openurl: (nsurl *) Url{ nslog (@ "hook_openurl:%@", [url Absolutestring]); return [self hook_openurl:url];}
#import "KHookObjectWrapper.h" #import "uikit/uikit.h" #import <objc/objc.h> #import <objc/runtime.h> @implementation KHookObjectWrapper+ (void) setup{ // Openurl method m = class_getinstancemethod ([UIApplication class], @selector (OpenURL:)); method m2 = class_getinstancemethod ([Self class], @selector (hook_openurl:)); class_addmethod ([ uiapplication class], @selector (hook_openurl:), method_getimplementation (m), method_ Gettypeencoding (m)); //Why should have this sentence, because UIApplication no Hook_openurl method will run collapse, we think can speak self hook_openurl renamed OpenURL, You can try it, it is not method_exchangeimplementations (M,&NBSP;M2);} - (BOOL) Hook_openurl: (nsurl *) Url{ nslog (@ "hook_openurl:%@", [url Absolutestring]); &NBSP;&NBSP;&NBSP;&NBsp;return [self hook_openurl:url];} @end
--------------------------------------------------------------------
Add a little, if you just want to rewrite some methods of the class, classification is also a good choice, classification once added to the project, do not need to include the header file will be effective, so please use carefully
@implementation uiapplication (Test)-(BOOL) OpenURL: (nsurl*) URL {NSLog (@ "!!!!! %@ ", [url absolutestring]); return YES;} @end
Of course you can't get back to the original IMP again!
IOS reverse engineering-Internal hooks (Method swizzling)