LoginSignup
17
16

More than 5 years have passed since last update.

Swift 2.0でのプッシュ通知の設定(registerUserNotificationSettings)

Posted at

はじめに

プッシュ通知の初期設定でUIUserNotificationSettingsの記述方法がSwift 2.0で変わっていたのでメモ。

Swift 1.2以前

let settings = UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()

この記述ではSwift 2.0以降では以下のようなエラーになります。

Binary operator '|' cannot be applied to two 'UIUserNotificationType' operands

Swift 2.0以降

UIUserNotificationTypeがOptionSetTypeになり記述方法が以下のように変わりました。

let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound,], categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(settings)
UIApplication.sharedApplication().registerForRemoteNotifications()

ちなみに、OptionSetTypeは以下のように型の名前まで書くこともできますが、.以降のみ記述する方が一般的のようです。

let settings = UIUserNotificationSettings(forTypes: [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound,], categories: nil)
17
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
16