1

Let's consider the following line of code:

reminder.calendar = appDelegate!.eventStore!.defaultCalendarForNewReminders()

this denotes that I put reminder into the default device's calendar. How can I create my own calendar from code?

The reason I want to create my own one is that I want to have possibility to manage all reminders there.

Filburt
  • 17,626
  • 12
  • 64
  • 115
theDC
  • 6,364
  • 10
  • 56
  • 98

1 Answers1

6

Create a new EKCalendar instance for the specific type, assign a title and a source and save it to the database

let calendar = EKCalendar(for: .reminder, eventStore: eventStore)
calendar.title = "MyNewCalendar"
calendar.source = eventStore.defaultCalendarForNewReminders()?.source
do {
    try eventStore.saveCalendar(calendar, commit: true)
} catch { print(error) }

Retrieving the reminder calendar is a bit tricky

var myCalendar: EKCalendar?
let calendars = eventStore.calendars(for: .reminder)
if let filteredCalendar = calendars.first(where: {$0.title == "MyNewCalendar"}) {
    myCalendar = filteredCalendar
} else {
    print("count not find reminder calendar 'MyNewCalendar'")
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks, I got one more question if you don't mind. I try to access this calendar this way: reminder.calendar = appDelegate!.eventStore!.calendarWithIdentifier("MyNewCalendar") and the result is: Reminder failed with error Optional("No calendar has been set.") – theDC Jul 16 '15 at 12:47
  • could you take a look if you don't mind? http://stackoverflow.com/questions/31456504/deleting-reminders-from-calendar-in-swift – theDC Jul 16 '15 at 14:15
  • Apple now recommends to add "where:" key before trailing closures so the code would change like : if let filteredCalendar = calendars.first(where: {$0.title == "MyNewCalendar"}) { myCalendar = filteredCalendar } – Ege Sucu Jan 10 '21 at 15:38