【问题标题】:Swift EKCalendar not persistingSwift EKCalendar 不持久
【发布时间】:2016-12-01 03:10:45
【问题描述】:

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

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 以外的其他来源,但是当我这样做时,日历甚至不会首先保存。以下是使用本地资源的尝试之一:

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

我也试过这个方法:

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/

还有其他人遇到过这个问题吗?

【问题讨论】:

    标签: calendar swift3 eventkit


    【解决方案1】:

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

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

    您正在执行第一步,但不是第二步,因此您的日历保留在商店中,但您没有保留将来检索它们所需的信息。

    【讨论】:

    • 我实际上确实将日历 ID 保存到了 UserDefaults,但它有点令人费解,所以我忽略了该代码。就像我说的,我可以给它们写事件,所以检索它们是没有问题的。问题是当我杀死应用程序,然后重新打开它时,即使使用来自 UserDefaults 的 ID,日历也找不到。
    • 嗯。 tbh 我没有使用此 API 的任何经验,但如果您在调用 saveCalendar(, commit:) 时没有发现任何错误,那么我觉得问题在于检索而不是持久性。您能否确认您已检查您有权访问日历 (EKEventStore.authorizationStatus(for: )) 返回 EKAuthorizationStatus.authorized
    • 我绝对有授权,因为我可以看到我在内置 Apple 日历应用程序中创建的日历以及我在上面创建的所有事件。我知道这是一个持久性问题,因为只有在应用程序在后台停留几秒钟后才会发现日历为零。在重新启动我的应用程序之前,我会检查 Apple 日历,看看日历和所有事件都消失了。
    • 这听起来......令人沮丧:(如果你想要第二双眼睛,我很乐意查看一个 git hub 项目。我看到这个注释有人注意到一些关于隐藏本地日历的奇怪行为启用 iCloud 日历时:stackoverflow.com/questions/33258997/… 这可能是原因吗?
    • 这是一场漫长的战斗,我非常感谢您的同情和帮助!那篇文章可能与我的问题相似,因为我们似乎都将日历保存到(可能)不一致的来源。该项目目前不在 gitHub 上,但已在待办事项列表中。我会看看我是否可以在某个时候给你一个 gitHub 链接。
    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多