Aspect principle analysis

Human memory is limited, and analysis, modeling and interpretation capabilities are unlimited

Call Hierarchy

isa hook

aspect_hookClass()

aspect_prepareClassAndHookSelector()

aspect_add()

aspect_hookSelector:withOptions: usingBlock:error:]

+[NSURLCache load]

Explain system redirection

aspect_swizzleForwardInvocation()

< p> aspect_hookClass()

aspect_prepareClassAndHookSelector()

aspect_add()

+[NSObject aspect_hookSelector:withOptions:usingBlock:error:]

+[NSURLCache load]

(Explanation) Forwarding system redirection

aspect_getMsgForwardIMP()

Aspect_prepareClassAndHookSelector()

aspect_add()

+[NSObject aspect_hookSelector:withOptions:usingBlock:error:]

+[NSURLCache load]

< p>

// We use forwardInvocation to hook in.

class_replaceMethod(klass, selector, aspect_getM sgForwardIMP(self, selector), typeEncoding);

Overall principle

I said above There are several message forwarding methods. Aspects mainly use forwardInvocation for forwarding. Aspects actually use a similar principle to kvo. By dynamically creating subclasses, the corresponding object isa pointer points to the created subclass, and then the subclass’s The IMP of forwardInvocation is replaced by __ASPECTS_ARE_BEING_CALLED__. Assuming the name of the method to be hooked is XX, add an Aspect_XX method to the subclass, and then point the IMP of Aspect_XX to the IMP of the original XX method, so that it is convenient to call the original method later, and then The IMP of hook method XX points to _objc_msgForward, which enters the message forwarding process, and the IMP of forwardInvocation is replaced with __ASPECTS_ARE_BEING_CALLED__, which will enter __ASPECTS_ARE_BEING_CALLED__ for interception processing, so that the entire process is about to end.

https://www.jianshu.com/p/0d43db446c5b

p>

Human memory is limited, analysis, modeling and interpretation are unlimited

Leave a Comment

Your email address will not be published.