Debugging NSNotifications on iOS

For iOS developers, this is a really cool technique.  Sometimes you want to see all the NSNotifications that get posted in your app, whether it is for debugging, or to see the timing of where listeners can be hooked in.  Using breakpoints in Xcode will allow us to inspect the notifications.

Xcode console
Some system notifications (both public and private), and some application notifications being generated at the boot of iTMAC

Follow this procedure to set it up:

  1. In Xcode, open the breakpoints panel on the left sidebar.
  2. Click the + icon at the bottom left of the panel.  Select “Add Symbolic Breakpoint…
  3. Enter the following details:
    Symbol: -[NSNotificationCenterpostNotificationName:object:userInfo:]
  4. Click Add Action and enter the following details:
    Debugger Command: po $r2
    This prints out the 3rd parameter (internally speaking) — the name of the notification.  The first two parameters if you’re curious are the NSNotificationCenter instance, and the command/message.
    Updated June 7, 2015: If you are getting error: use of undeclared identifier '$r2' try using po $arg3 instead.
  5. Select “Automatically continue after evaluating“.

Your breakpoint settings should look like this:
Xcode NSNotification breakpoint settings

You will then need to run the application on a device (sorry, this doesn’t work on the simulator). Note that if your application produces many notifications constantly, the speed of the application will be significantly slower, due to the breakpoints being processed. To work around this, you can disable the breakpoint until you get to the place where you want to analyze the notifications.

Related

4 thoughts on “Debugging NSNotifications on iOS

  1. This is pretty cool! I’ve seen people debug NSNotifications in a lot of ways, and I think this is one of the more creative ones. Nice work Dennis. If you’re interested, I wrote an app called the Spark Inspector (http://www.sparkinspector.com/) that comes with a tool for inspecting notifications in your app (tracing where they’re sent from / who’s observing them, etc…) If you’d like to give that a try, I’d be happy to send you a free copy.

    Good stuff! I’m bookmarking your blog 😉

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.