gpt4 book ai didi

ios - EventKit 从日历中删除事件

转载 作者:行者123 更新时间:2023-11-30 11:29:13 32 4
gpt4 key购买 nike

这是我将所有事件添加到日历中的方式,这些事件来自表格 View 。当 TableView 上的行被删除时,我在从日历中删除特定的偶数时遇到问题。我正在尝试的代码似乎在日历中找不到和标识符。你能告诉我我在这里缺少什么

添加到日历

let eventStore : EKEventStore = EKEventStore()

// 'EKEntityTypeReminder' or 'EKEntityTypeEvent'

eventStore.requestAccess(to: .event) { (granted, error) in

if (granted) && (error == nil) {
print("granted \(granted)")
print("error \(error)")

let event:EKEvent = EKEvent(eventStore: eventStore)

event.title = "Test Title"
event.startDate = Date()
event.endDate = Date()
event.notes = "This is a note"
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.save(event, span: .thisEvent)
} catch let error as NSError {
print("failed to save event with error : \(error)")
}
print("Saved Event")
}
else{

print("failed to save event with error : \(error) or access not granted")
}
}

从日历中删除

func deleteEvent(_ storedEventID: String)
{
eventStore.requestAccess(to: .event, completion: { (granted, error) in
if (granted) && (error == nil)
{

if let calendarEvent_toDelete = self.eventStore.event(withIdentifier: storedEventID){

//recurring event
if calendarEvent_toDelete.recurrenceRules?.isEmpty == false
{
let alert = UIAlertController(title: "Repeating Event", message:
"This is a repeating event.", preferredStyle: UIAlertControllerStyle.alert)

//delete this event only
let thisEvent_Action = UIAlertAction(title: "Delete this event", style: UIAlertActionStyle.default)
{
(result : UIAlertAction) -> Void in

//sometimes doesn't delete anything, sometimes deletes all reccurent events, not just current!!!
do{
try self.eventStore.remove(calendarEvent_toDelete, span: .thisEvent)
} catch let e as NSError{return}

}


alert.addAction(thisEvent_Action)

}
//not recurring event
else{
//works fine
do{
try self.eventStore.remove(calendarEvent_toDelete, span: EKSpan.thisEvent)
} catch let e as NSError{
return
}
}
}

}
})
}

最佳答案

我在您的示例中缺少的是将更改提交到事件存储。

立即提交更改或在批量处理多个事件时单独提交。

try? self.eventStore.remove(eventToRemove, span: .thisEvent, commit: true)

祝你好运并成功。

关于ios - EventKit 从日历中删除事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50425744/

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