Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ArtOfWarfare

macrumors G3
Original poster
Nov 26, 2007
9,710
6,304
Hi,

What's the best way of determining if a certain feature is available?

I know you can check whether certain methods of a class exist, but that requires the class itself to exist, does it not? How can I determine if that class exists?

Right now, I'm thinking I'll try using gestaltSystemVersionMinor, but there's a pretty clear note in the documentation against doing that:

Important Never infer the availability of hardware or software features from the value that the Gestalt function returns when you pass it the gestaltSystemVersion selector.

Specifically, I'm wondering about (User) Notification Center. If it's available, I'd like to use it; if it's not available, I'd like to fall back on alert windows.
 
Last edited:
This should work for deployment targets from 10.6.8:

Code:
if ([NSUserNotificationCenter class]) { 

}

Documentation on it seems quite sparse, but is the preferred method of checking class availability. The best explanation of it I've found is in the WWDC 2011 session on Objective-C advancements.
 
As per the Apple documentation on the subject this is the correct way to do it.

That document is out of date, and explicitly says it's not possible with OS X:

In Mac OS X (and in iOS projects that do not meet the set of conditions just listed), you cannot use the class method to determine if a weakly linked class is available.

The only mention in official documentation I could find was the 10.7 release notes which doesn't really provide any useful details. I've sent feedback on the document you linked to, so will hopefully it will be updated.
 
Last edited:
That document is out of date, and explicitly says it's not possible with OS X:



The only mention in official documentation I could find was the 10.7 release notes which doesn't really provide any useful details. I've sent feedback on the document you linked to, so will hopefully it will be updated.

Shows what I get for linking something I remember from ages ago without re-reading it :eek:
 
This is the standard, preferred way of doing it:

Code:
Class klass = NSClassFromString(@"NSUserNotificationCenter");
if (klass) {
    // Bingo
} else {
    // Nope
}

Edit: As mentioned above, there is new support for using the

Code:
[NSUserNotificationCenter class]

method, but it is a newer feature and is not supported on all versions of OS X / iOS:

https://developer.apple.com/library/ios/#releasenotes/General/WhatsNewIniPhoneOS/Articles/iOS4.html#//apple_ref/doc/uid/TP40010313-SW5

It should really work everywhere. [NSUserNotificationCenter class] attempts to send the class method named "class" to the object NSUserNotificationCenter. Because the object doesn't exist, it will be nil, and sending a message to a nil object returns nil reliably.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.