1

I can create a new calendar and save it with the following function:

func createCalendarForUser() {
    let sourcesInEventStore = self.eventStore.sources

    //works but doesn't persist
    let subscribedSourceIndex = sourcesInEventStore.index {$0.title == "Subscribed Calendars"}
    if let subscribedSourceIndex = subscribedSourceIndex {
        let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
        userCalendar.title = "newCalendar"
        userCalendar.source = sourcesInEventStore[subscribedSourceIndex]
        do {
            try self.eventStore.saveCalendar(userCalendar, commit: true)
            print("calendar creation successful")
        } catch {
            print("cal \(userCalendar.source.title) failed : \(error)")
        }
    }
}

This functions great while the app is open and running. I can save events to them, i can see them in my local calendar, and life is good. However, once the app is terminated and goes into the background the calendars disappear, along with any events created in them. I've tried saving the calendar to different sources other then the Subscribed Calendars source but when i do that the calendars wont even save in the first place. Heres one of the attempts at using the local source:

func createCalendarForUser() {
    let sourcesInEventStore = self.eventStore.sources

    //never saves calendar
    let localSourceIndex = sourcesInEventStore.index {$0.sourceType == .local}
    if let localSourceIndex = localSourceIndex {
        let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
        userCalendar.title = "newCalendar"
        userCalendar.source = sourcesInEventStore[localSourceIndex]
        do {
            try self.eventStore.saveCalendar(userCalendar, commit: true)
            print("creating new calendar successful")
        } catch {
            print("creating new calendar failed : \(error)")
        }
    }
}

I also tried this method :

func createCalendarForUser() {
    let sourcesInEventStore = self.eventStore.sources

    //doesnt work
    let userCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
    userCalendar.title = "newCalendar"
    userCalendar.source = sourcesInEventStore.filter{
        (source: EKSource) -> Bool in
        source.sourceType.rawValue == EKSourceType.local.rawValue
        }.first!
    do {
        try self.eventStore.saveCalendar(userCalendar, commit: true)
        print("creating new calendar succesful")
    } catch {
        print("creating new calendar failed : \(error)")
    }
}

As metioned here as mentioned here https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/

Has anyone else come across this problem?

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

1 Answers1

0

The blog post you link do does two things when it saves the calendar, it uses the saveCalendar(, commit) method to save the calendar to the event store, and then also saves the identifier for the calendar to user defaults so that it can be retrieved at a later time:

NSUserDefaults.standardUserDefaults().setObject(newCalendar.calendarIdentifier, forKey: "EventTrackerPrimaryCalendar")

You're doing the first, but not the second step, so your calendars will be persisting in the store, but you're not keeping the information needed to retrieve them in the future.

MathewS
  • 2,267
  • 2
  • 20
  • 31
  • I actually do save the calendar IDs to the UserDefaults, but it's slightly convoluted so I left that code out. Like I said, I can write events to them, so retrieving them is no problem. The problem is when I kill the app, and then reopen it, the calendars are found nil even when using the ID from the UserDefaults. – Dustin Spengler Jan 07 '17 at 03:42
  • Hmm. tbh I don't have any experience working with this API, but if you're not catching any errors calling `saveCalendar(, commit:)` then it feels to me that the problem is retrieval rather than persistence. Can you confirm that you check you have authorization to access the calendars (`EKEventStore.authorizationStatus(for: )`) returns `EKAuthorizationStatus.authorized`? – MathewS Jan 07 '17 at 03:58
  • I definitely have authorization because I can see the calendars I create in the built in Apple Calendar app as well as all the events i have created on them. I know its a persistence problem because the calendars are only found nil after the app has sat in the background for a few seconds. Before relaunching my app I will check the Apple Calendar and see that the calendars and all the events are gone. – Dustin Spengler Jan 07 '17 at 04:08
  • That sounds... frustrating :( If you want a second pair of eyes I'm happy to look over a git hub project. I saw this note someone noticed some weird behavior about local calendars being hidden when iCloud calendars are enabled: http://stackoverflow.com/questions/33258997/objective-c-ical-not-creating-custom-calendar-and-new-event-to-it-in-ios-9 could that be a possible reason? – MathewS Jan 07 '17 at 04:32
  • Its been a long battle and i really appreciate your sympathy and help! That post could be similar to my problem because it seems like we're both saving the calendars to (possibly) inconsistent sources. The project isnt currently on gitHub but its been on the toDo list. Ill see if i can PM you a gitHub link at some point. – Dustin Spengler Jan 07 '17 at 05:00