We’re updating our iOS application at work, and I came across some fun with iOS 8 and notifications. So, I thought I’d write a short post about it.

In iOS 7, we used the following method on the shared UIApplication instance to set up notifications:

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(
)];


In iOS 8, this doesn’t work. So now, it’s a multi-step process:

UIApplication *application = [UIApplication sharedApplication];

categories:nil];


First, we get an instance of the shared UIApplication (this is obviously optional, but I think it makes the code easier to read). Then, we create a UIUserNotificationSettings instance that specifies which notifications we want. After that, we register for user notifications, and finally, register for remote notifications.

The new method, registerForRemoteNotifications:, sends the same messages as the registerForRemoteNotificationTypes: method. These are application:didRegisterForRemoteNotificationsWithDeviceToken: for a successful registration, and application:didFailToRegisterForRemoteNotificationsWithError: for an error.

Well, what if you want to support both iOS 7 and iOS 8? It’s simple. See if the UIApplication instance responds to the new selector. Here’s the complete code:

UIApplication *application = [UIApplication sharedApplication];

{
categories:nil];