Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

UNCalendarNotificationTrigger won't work with full date components

In an earlier question I mentioned a problem scheduling local notifications. The real cause for the problem was that I used to following code to create the trigger, which does not work:

let calendar = ...
let notificationDate = ...

let dateComponents = calendar.dateComponents(in: calendar.timeZone, from: notificationDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

The following code does work, however:

let calendar = ...
let notificationDate = ...

let dateComponents = calendar.dateComponents([.day, .month, .year, .hour, .minute], from: notificationDate)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

So the question is: What's happening here? Why does the latter work while the former does not?

Upvotes: 3

Views: 1173

Answers (1)

Schemetrical
Schemetrical

Reputation: 5536

It seems to be that if you provide date components that are way too specific, it won't match for the trigger. It says in the documentation to "Provide only the date components that are relevant for your trigger."

I ran into this issue too, and I found that [.day, .month, .year, .hour, .minute, .second] is usually safe.

Upvotes: 4

Related Questions