Swift EKCalendar not persisting

2019-06-05 05:40发布

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?

1条回答
叼着烟拽天下
2楼-- · 2019-06-05 06:35

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.

查看更多
登录 后发表回答