Method Swizzling principle
When you call a method in Objective-C, you’re actually sending a message to an object, and the only way to find the message is by the name of the selector. By taking advantage of the dynamic characteristics of Objective-C, the corresponding method implementation of selector can be swapped at run time to achieve the purpose of hook method. Each class has a list of methods that hold the name of the selector and the mapping between the method implementation. IMP is a bit like a function pointer to a specific Method implementation.
We can use method_exchangeImplementations to swap IMPs in two methods,
We can modify the class using class_replaceMethod,
We can use method_setImplementation to set the IMP of a method directly…
In the end, they are all imPs that have swapped their selectors, as shown below:
Read the original