Swift EKCalendar not persisting

只谈情不闲聊 提交于 2019-12-08 07:00:31

问题


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:


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.



来源:https://stackoverflow.com/questions/40902013/swift-ekcalendar-not-persisting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!