Sending a message in Objective-C is translated into a call of the function objc_msgSend(receiver, selector, arguments)
or one of its variants objc_msgSendSuper
, objc_msgSend_stret
, objc_msgSendSuper_stret
.
If it was possible to change the implementation of these functions, we could intercept any message. Unfortunately, objc_msgSend
is part of the Objective-C runtime and cannot be overridden.
By googling I found a paper on Google Books: A Reflective Architecture for Process Control Applications by Charlotte Pii Lunau. The paper introduces a hack by redirecting an object's isa
class pointer to an instance of a custom MetaObject class. Messages that were intended for the modified object are thus sent to the MetaObject instance. Since the MetaObject class has no methods of its own, it can then respond to the forward invocation by forwarding the message to the modified object.
The paper does not include the interesting bits of the source code and I have no idea if such an approach would have side effects in Cocoa. But it might be interesting to try.