iOSエンジニアのつぶやき

毎朝8:30に iOS 関連の技術について1つぶやいています。まれに釣りについてつぶやく可能性があります。

【Swift】Date から UNCalendarNotificationTrigger を生成するメモ

Local Notification を設定する際に、指定した日時でトリガーされるようにした時の UNCalendarNotificationTrigger の生成方法をメモしておきます🦅

結論

結論としては下記のようになります。日時でトリガーする際に必要な UNCalendarNotificationTriggerDateComponents を使用することで初期化できるので、指定の DateCalendar.current.dateComponents(in: .current, from: soberDate) に渡すことで DateComponents を取得することができます。

        let content = UNMutableNotificationContent()
        content.title = "Titileだよ"
        content.body = "Bodyだよ"
        content.sound = UNNotificationSound.default

        let components = Calendar.current.dateComponents(in: .current, from: hogeDate)
        return UNNotificationRequest(identifier: "hoge_hoge", content: content, trigger: UNCalendarNotificationTrigger(dateMatching: components, repeats: false))

という感じで本日は以上になります🍺

参考

その他の記事

yamato8010.hatenablog.com

yamato8010.hatenablog.com

yamato8010.hatenablog.com