OGeek|极客世界-中国程序员成长平台

标题: ios - 创建新的 EKCalendar iOS 的适当情况 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:34
标题: ios - 创建新的 EKCalendar iOS 的适当情况

我正在开发一个创建、更新和删除 EKEvents 的 iOS 应用程序。这可以通过将事件保存到 EKEventStore.defaultCalendarForNewEvents 轻松完成。在什么情况下我想为我自己的应用创建一个新的 EKCalendar,这需要什么功能?

我之所以问,是因为我目前正在尝试在 Swift 3.0 中创建一个日历,但它一直失败,这让我一开始就想知道新日历的目的是什么。

fileprivate var eventStore = EKEventStore()
fileprivate var newCalendar : EKCalendar?

func createNewCalendar() {
    self.newCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
    self.newCalendar!.title = "newCal"
    let sourcesInEventStore = self.eventStore.sources
    self.newCalendar!.source = eventStore.defaultCalendarForNewEvents.source

    let newCalIndex = sourcesInEventStore.index {$0.title == "newCal"}
    if newCalIndex == nil {
        do {
            try self.eventStore.saveCalendar(self.newCalendar!, commit: true)
            print("cal succesful")
        } catch {
            print("cal failed")
        }
    }
}

我知道我可以访问 eventStore,因为我可以提取事件并将它们保存到 EKEventStore.defaultCalendarForNewEvents 并使用现有日历更新它们。



Best Answer-推荐答案


您想要创建新日历的原因可能有很多。就个人而言,当我想将一组事件与已创建并绑定(bind)到默认事件的事件分开时,我会选择创建一个新日历。通过这种方式,当您认为不需要它们时,您还可以利用批量删除这些新创建的事件。只需删除日历,其所有事件也将被清除。

顺便说一句,如果你不确定你想要的日历的 source (iCloud,本地,可能绑定(bind)到某些邮件帐户等)要创建,只需使用默认的来源:

let newCalendar = EKCalendar(forEntityType: .Event, eventStore: eventStore)
newCalendar.source = eventStore.defaultCalendarForNewEvents.source

如果你想使用它的源,还要确保默认日历的 allowsContentModifications 属性返回 true 否则你很可能无法创建一个新日历中的事件。

关于ios - 创建新的 EKCalendar iOS 的适当情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39584928/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4