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.
Thanks for the examples with code
.
I was expecting more in this release of OS X from Apple
.
There are a few APIs like tabs and the token search field that is in mail, that I thought were certain to be in Mountain Lion, but unfortunately not
Lion seemed like a big release for developers and Mountain Lion seems like it is more for users.
You can choose to have Notification Center organize your notifications in one of two ways: manually or by time. In the default manual scheme, you can open the Notifications pane in System Preferences and drag the apps and services in the left hand column into whatever order you desire. If you choose to instead organize them by time, which you can do via the pop-up menu at the bottom of the preference pane, you’ll instead see notifications arrive with the most recent at the top. For each app, you can choose to see 1, 5, 10, or 20 of the most recent items, as well as decide whether the app should send banner or alert notifications (or none at all), whether an icon should be badged with unread messages, and whether a sound should play.