Search code examples
iosxcodeswiftios7apple-push-notifications

Local Notifications in Swift Crashing on iOS 7


I am trying to add local notifications to my game. The code works with iOS 8 but when I try to test it on iOS 7 it just crashes.

This is the code to register the notifications(its in the App Delegate)

let notificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound
let settings = UIUserNotificationSettings(forTypes: notificationType, categories: nil)
application.registerUserNotificationSettings(settings)

And this is my code to set the notification

UIApplication.sharedApplication().cancelAllLocalNotifications()

notification.fireDate = NSDate(timeIntervalSinceNow: 3600)
notification.alertBody = "Come back and play"
notification.alertAction = "to play"
notification.applicationIconBadgeNumber = 1

UIApplication.sharedApplication().scheduleLocalNotification(notification)

It crashes when its registering the notifications.


Solution

  • In iOS 7 registerUserNotificationSettings is an unrecognized selector and will crash. You need to see if it responds to that selector and if it doesn't call registerForRemoteNotificationTypes: instead.