Displaying all notifications

I’ve started to use NSNotifications a lot lately, to communicate between multiple objects on screen at the same time. I’ve found them to be extremely handy when dealing with multiple buttons in multiple panels that have to have cross-communication (or drag-and-drop functionality). This is especially useful on the iPad, where there is so much more real-estate for interfaces.

One thing that Justin Leger and I discovered today, was an easy method for logging all notifications that are fired within an app. (We were curious what notifications might be sent when a Youtube video was launched from a UIWebView, and there was no documentation about it.)

We started, by simply observing “nil”:

	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(dumpAllNotifications)
												 name:nil
											   object:nil];

This called dumpAllNotifications, which looked like:

- (void) dumpAllNotifications {
	NSLog (@"%@", [sender name]);
}

We also decided to filter the notifications by preceding the NSLog command with:

	if ([[sender name] hasPrefix:@"UI"])

(this way, we’d only see the UI related notifications)

Once we found the names, we were able to look to see if they were documented — knowing the name of the notification you need is much more useful than trying to craft the proper google search term in the dark. Unfortunately, there are a number of undocumented notifications that probably shouldn’t be used — not only because Apple discourages it, but because, being undocumented, they could completely change in the future. This is definitely not something to rely upon, but it’s still a great way to learn more about the internals of our software.

jeffrey

About jeffrey

Jeffrey Berthiaume is a multimedia developer and internet architect who has designed and built award-winning websites, kiosks, and content management systems. He bridges the gap between creative and technology with an ability to balance the needs of designers and marketing with the capabilities of existing technology.
This entry was posted in Development, iOS, Tutorials. Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>