Swift EKCalendar无法持续 [英] Swift EKCalendar not persisting

查看:83
本文介绍了Swift EKCalendar无法持续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创建一个新日历并使用以下功能保存它:

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)")
        }
    }
}

在应用程序打开并运行时,此功能非常有用。我可以将事件保存到它们,也可以在本地日历中看到它们,生活很美好。但是,一旦该应用终止并进入后台,日历以及在其中创建的所有事件都会消失。我曾尝试将日历保存到 Subscribed Calendars 源之外的其他来源,但是当我这样做时,日历甚至都不会保存。以下是使用本地源的一种尝试:

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)")
        }
    }
}

我也尝试过这种方法: / p>

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)")
    }
}

如此处所述 https://www.andrewcbancroft.com/2015/06/17/creating-calendars-with-event-kit-and-swift/

有有人遇到这个问题吗?

Has anyone else come across this problem?

推荐答案

您链接的博客文章在保存日历时会做两件事,它使用 saveCalendar(,commit)方法将日历保存到事件存储,然后还将日历的标识符保存到用户默认值,以便以后可以检索:

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.

这篇关于Swift EKCalendar无法持续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆