LoginSignup
7
3

More than 3 years have passed since last update.

【iOS14】UNUserNotificationCenterでSwitch must be exhaustiveのwarning対応

Last updated at Posted at 2020-09-16

Xcode12(GM)でビルドするとSwitch must be exhaustiveのwarningが👀
実装を確認すると、通知許可ステータスを確認するコードで吐き出されている様子

        UNUserNotificationCenter.current().getNotificationSettings { settings in
            switch settings.authorizationStatus {
            case .authorized, .provisional:
                print("許可")
            case .denied:
                print("拒否")
            case .notDetermined:
                print("許可求む")
            @unknown default:
                fatalError()
            }

UNAuthorizationStatusのリファレンスを覗くとiOS14から新たに
.ephemeral
が追加されていました。
https://developer.apple.com/documentation/usernotifications/unauthorizationstatus

この子は
The application is temporarily authorized to post notifications. Only available to app clips.
つまり、iOS14から実装可能なApp Clipsにおいて、一時的に通知の許可を取ることができ、そのステータスということです。

App Clipsを実装している場合の実装はこちらの公式リファレンスを参考に
https://developer.apple.com/documentation/app_clips/enabling_notifications_in_app_clips

let center = UNUserNotificationCenter.current()
center.getNotificationSettings { (settings) in {
    if settings.authorizationStatus == .ephemeral {

        // The user didn't disable notifications in the app clip card.
        // Add code for scheduling or receiving notifications here.
        return
    }
}

などを追加します。

App Clipsを実装していない場合は、通らない想定なので

        UNUserNotificationCenter.current().getNotificationSettings { settings in
            switch settings.authorizationStatus {
            case .authorized, .provisional:
                print("許可")
            case .denied:
                print("拒否")
            case .notDetermined:
                print("許可求む")
            case .ephemeral:
                fatalError()// or assertionFailure()
            @unknown default:
                fatalError()
            }

でwarning回避できます。
リファレンスにbetaと書かれているし、fatalError利用したくない方は念の為
assertionFailureで、取得失敗の扱いで良いかと思います🙋‍♂️

ご参考までに🐣🌱

7
3
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
7
3