gpt4 book ai didi

ios - 适当情况下创建新的EKCalendar iOS

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:57:29 25 4
gpt4 key购买 nike

我正在开发一个创建、更新和删除 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 并使用现有日历更新它们。

最佳答案

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

顺便说一下,如果您不确定您想要的日历的来源 (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com