Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build an app that runs on Mac OS 10.6 and supports the NSUserNotificationCenter?

I did a lot of research but I just couldn't find what I'm looking for: Currently, both the Deployment Target and Base SDK of my app is set to 10.8, and it runs perfectly. I can properly post Notifications to Mountain Lions new Notification Center!

To build an app that also runs under Mac OS 10.6, I've set the Deployment Target to 10.6 and built it (as recommended in this thread). Unfortunately, the app crashes on 10.6, and the crash report is detailing that the NSUserNotificationCenter couldn't be found (No OS version handling).

--

So what I surely have to do, is to check, whether the NSUserNotificationCenter class exists:

notificationCenterIsAvailable = (NSClassFromString(@"NSUserNotificationCenter")!=nil);

and operate based on this boolean flag.

But what about Code that can't be based on this flag, like:

  • the interface ..: <NSUserNotificationCenterDelegate> {
  • or the type of the reference to the Notification Center: NSUserNotificationCenter *center in the class' properties?

My first thought was to dynamically alter the class header at runtime, i.e., add the methods and properties to handle the Notificaion Center based on whether the class exists or not, but this seems like really complex ?!

like image 992
Raffael Avatar asked Jul 31 '12 09:07

Raffael


1 Answers

Those are compilation issues and will be fine all the time the Base SDK is 10.8. The center pointer can remain at nil if NSUserNotificationCenter does not exist, and you could therefore use that as a flag to check if you are doing notifications or not in code run after the test.

EDIT In fact leaving it nil will mean you don't even need to check for it; just send messages as normal and they will be ignored if the pointer is nil (this is unless the creation of the message is expensive and you want to save some CPU cycles).

like image 69
trojanfoe Avatar answered Nov 04 '22 00:11

trojanfoe