Search code examples
iosswiftreminders

Reminder added through app not showing alarm time


I am in the process of writing an app that allows users to add a reminder with an alarm time. The reminder is working correctly and the alarm is going off when it should, but when I go into the Reminders app and look at the reminder that was set in my app, the alarm text is not appearing under the reminder name in the UITableViewCell.

Here is the Reminders app showing a reminder set through the Reminders app and one set through my app:

enter image description here

Even though the text is not showing up in the cell, if I edit the reminder, the alarm is set correctly:

enter image description here

Right now, there's not much to the app - I am just getting started. Here's the screen that I'm currently using to add the reminder.

enter image description here

This is the code that is being used to add the reminder and alarm:

func createReminder() {

    let reminder = EKReminder(eventStore: appDelegate.eventStore!)

    reminder.title = reminderText.text!
    reminder.calendar =
        appDelegate.eventStore!.defaultCalendarForNewReminders()
    let date = myDatePicker.date
    let alarm = EKAlarm(absoluteDate: date)

    reminder.addAlarm(alarm)

    do {
        try appDelegate.eventStore?.save(reminder,
                                         commit: true)
    } catch let error {
        print("Reminder failed with error \(error.localizedDescription)")
    }
}

What could I be missing?


Solution

  • I think I understand your issue. I just used your code to create a reminder and it just created a reminder without the date in the subtitle.

    What we need to understand is setting an alarm date != the due date of the reminder. Users can set a due date/timing and add reminders before the due date. Read the documentation for more information.

    To add the date you need to set the dueDateComponents property of the EKReminder object that you pass to eventStore. Like below,

    let reminder = EKReminder(eventStore: eventStore)
    reminder.title = reminderText
    reminder.calendar = eventStore.defaultCalendarForNewReminders()
    
    let date = Date().addingTimeInterval(10000)
    let alarm = EKAlarm(absoluteDate: date)
    reminder.addAlarm(alarm)
    
    let dateComponents = NSDateComponents();
    dateComponents.year = 2018
    dateComponents.day = 14
    dateComponents.hour = 11
    dateComponents.month = 1
    reminder.dueDateComponents = dateComponents as DateComponents
    
    do{
         try eventStore.save(reminder, commit: true)
    } catch let error {
         print("Reminder failed with error \(error.localizedDescription)")
    }
    

    The screenshot below shows 4 events. 1st and the last were created manually, the second was created from your code without dueDateComponents and the third one was created with dueDateComponents.

    The one with the red one was created with dueDateComponents