Search code examples
swift3eventkitreminders

Swift 3 Create Reminder EKEventStore


I would like to save reminders to the default reminders location. But when I press my button I get a fatal error: unexpectedly found nil while unwrapping an Optional value... I am pretty new to this and most examples I locate are overly complicated or not in Swift 3.

class ViewController: UIViewController {

var eventStore: EKEventStore?

@IBOutlet weak var reminderText: UITextField!

@IBAction func setReminder(_ sender: Any) {

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

    reminder.title = "Go to the store and buy milk"
    reminder.calendar = (eventStore?.defaultCalendarForNewReminders())!

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

   } 
 }

Solution

  • With the @adamprocter sample, we also need to add "NSRemindersUsageDescription" key with your message in info.plist file. I tried adding this as a comment but I am not eligible.