Last Updated: February 25, 2016
· bontojr

Log All Messages in Objective-C

Sometimes we really need to log all messages that are sent in our applications, for iOS or Mac OS. Unfortunately this is not really simply to do and Apple disabled some cool stuff in iOS to prevent a reverse engineer of their frameworks.

I discovered a great solution a couple of months ago that works for Mac OS and iOS (Simulator Only).

There's a very nice method in runtime.h that starts a complete logging of all messages called in runtime. The first step is, of course, import the library with #import <objc/runtime.h>. Once the library has been imported we have to use the following methods to start and stop the logging:

(void)instrumentObjcMessageSends(YES); //to start
(void)instrumentObjcMessageSends(NO); //to stop

We can place these 2 methods whenever we want.

The second step is read the log file. This system doesn't address the logging text inside our Xcode console, but in a file called msgSends-XXXX inside our tmp folder. So, what we've to do, is open the Terminal and use this command:

tail -100f /tmp/msgSends-XXXX

where XXX is the number of our latest log file and 100 is the number of lines we wants to display.

I hope this approach could help you in debugging your code.