2

I am working on an iOS app which creates, updates, and deletes EKEvents. This can easily be accomplish by saving the events to EKEventStore.defaultCalendarForNewEvents. Under what circumstance would I want create a new EKCalendar for my own app and what functionality does that entail?

I am asking because I am currently trying to create a calendar in Swift 3.0 and it keeps failing, which leaves me wandering what the purpose of the new calendar is in the first place.

fileprivate var eventStore = EKEventStore()
fileprivate var newCalendar : EKCalendar?

func createNewCalendar() {
    self.newCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
    self.newCalendar!.title = "newCal"
    let sourcesInEventStore = self.eventStore.sources
    self.newCalendar!.source = eventStore.defaultCalendarForNewEvents.source

    let newCalIndex = sourcesInEventStore.index {$0.title == "newCal"}
    if newCalIndex == nil {
        do {
            try self.eventStore.saveCalendar(self.newCalendar!, commit: true)
            print("cal succesful")
        } catch {
            print("cal failed")
        }
    }
}

I know i have access to the eventStore because I can pull in events as well as save them to the EKEventStore.defaultCalendarForNewEvents and update them using their existing calendar.

Dustin Spengler
  • 5,478
  • 4
  • 28
  • 36

1 Answers1

0

There might be many reasons why you want to create a new calendar. Personally, I choose to create a new calendar when I want to separate a group of events from the ones that were created and tied to the default one. By this way, you also take advantage of bulk deleting of those newly created events when you think you don't need them. Just delete the calendar and all of its events will be purged as well.

By the way, if you are not sure what should be the source (iCloud, local, maybe tied to some mail account etc.) of the calendar you want to create, just use the source of default one:

let newCalendar = EKCalendar(forEntityType: .Event, eventStore: eventStore)
newCalendar.source = eventStore.defaultCalendarForNewEvents.source

Also make sure that the default calendar's allowsContentModifications property returns true if you want to use its source otherwise it is very likely that you won't be able to create an event within the new calendar.

Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
  • 1
    Have you tested this code? I've been trying this for a few hours and every time it tries to save it fails. I even copied and pasted your code verbatim to no avail. – Dustin Spengler Sep 20 '16 at 03:37
  • What's the value of `eventStore` in your program? It must be an `EKEventStore` instance, not the class object. Please show us the code and the error message in your question. – Ozgur Vatansever Sep 20 '16 at 03:38
  • Have you faced an error or a warning in debug pane? Did you get user's permission first via calling `eventStore.requestAccessToEntityType(.Event)` – Ozgur Vatansever Sep 20 '16 at 03:46
  • just printed out the error: `Error Domain=EKErrorDomain Code=17 "That account does not allow calendars to be added or removed." UserInfo={NSLocalizedDescription=That account does not allow calendars to be added or removed.}` – Dustin Spengler Sep 20 '16 at 03:47
  • I think it is related to [this issue](http://stackoverflow.com/questions/19721553/adding-a-new-calendar-programatically-to-calendar-app-if-user-is-using-non-ios-a). You might want to use a different `source` value such as `local` or `iCloud`. – Ozgur Vatansever Sep 20 '16 at 03:51
  • Turns out its related to Gmail not wanting to sync up. Just curious, if i save an event to a new calendar which i created, will it be visible in my gmail (which is synced)? – Dustin Spengler Sep 20 '16 at 04:01