iOS 8: Notifications Changes

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:

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

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:

Let me know if you have any questions or if there’s a better way to do this in the comments.

EDIT: I originally messed up the if statement in the complete code above. I used registerUserNotificationSettings instead of registerUserNotificationSettings: which caused the statement to be false, even on iOS 8.

Leave a Reply

Your email address will not be published. Required fields are marked *