OS X Mountain Lion API Highlights

With Mountain Lion appearing in the AppStore just 1 year after its predecessor Lion (371 Days to be precise), it should come as no great surprise that there isn’t the large amount of new APIs that usually comes along with a major update to OS X. Nevertheless there are a few stand out APIs that are worth pointing out.

Notification Center

Notification Center was a major feature of iOS 5 and now it makes its way to the Mac in Mountain Lion. If your coming from iOS using UILocalNotification or if your coming from Growl! on OS X you should have no problems getting up to speed with notifications, which on the Mac are referred to as NSUserNotification.

To post a notification you will need to create a NSUserNotification object and set the title and optionally the subtitle and/or informative text.

NSUserNotification *userNotification = [[NSUserNotification alloc] init];

userNotification.title = notificationTitle;
userNotification.subtitle = notificationSubtitle;

If your notification is actionable you can set the text of 2 additional buttons using the actionButtonTitle and otherButtonTitle properties.

Like UILocalNotification you can set a notification to be shown at a given time (e.g. for an alarm) using the deliveryDate property.

When you have finished configuring your notification simply tell the NSUserNotificationCenter to deliver it:

[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:userNotification];
[userNotification release];

By default notifications are only shown if your application is not in the foreground, which may seem confusing at first as Xcode always show notifications regardless of wether it is in the foreground or not.

Thankfully all you have to do is become the delegate of the user notification centre:

[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:(id)self];

Then respond to the should present notification delegate method:

- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
    return YES;
}

It is important to note that only signed Apps (Gatekeeper, MAS etc) can post notifications.

Sharing

iOS has always had the ability to share photos and videos by E-mail and SMS and more recently by Twitter, it is this system wide sharing that NSSharingService tries to solve.

You can present your own UI for sharing, but most of the time you will want to use NSSharingServicePicker which you can present from a button.

The first thing you will need to do is to configure the button to send its action on mouse down

[shareButton sendActionOn:NSLeftMouseDownMask];

To share files you need to know their file system URLs and simply pass these to NSSharingServicePicker. If you present this from the button the OS will take care of everything for you:

NSSharingServicePicker *sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:fileURLs];

[sharingServicePicker showRelativeToRect:[sender bounds]
                          ofView:sender
                   preferredEdge:NSMinYEdge];
[sharingServicePicker release];

You can add your own sharing services in your app to the sharing service picker, but it is impossible for you to add your service to the OS and therefore make available for other apps to use.

Game Center

If you are familiar with Game Center on iOS you will be familiar with Game Center on OS X. Matchmaking, Achievements and your Friends are all here for your multiple device gaming pleasure.

iCloud

In Mountain Lion Apple has introduced the iCloud Document Browser, which means that all apps have standard way of displaying their Documents in the Cloud.

EventKit

EventKit comes directly from iOS and replaces the Calendar Store for interacting with a user’s calendars, events and reminders. The major benefit of Event Kit is that you can use the exact same code to interact with calendars on both iOS and OS X.