简体   繁体   English

EventKit 从日历中删除事件

[英]EventKit remove event from calendar

This is the way I am adding all the events in to my calendar, those events are coming from table view.这就是我将所有事件添加到我的日历中的方式,这些事件来自表格视图。 I have a problem with deleting a specific even from the calendar when the row on the table view gets deleted.当表视图上的行被删除时,我什至无法从日历中删除特定内容。 The code that I am trying seems not to find and identifier in the calendar.我正在尝试的代码似乎没有在日历中找到和标识符。 Can you please let me know what I am missing here你能告诉我我在这里缺少什么吗

ADD TO CALENDAR添加到日历

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

DELETE FROM CALENDAR从日历中删除

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

        }
    })
}

What I am missing in your example is to commit the changes to the event store. 我在您的示例中缺少的是将更改提交到事件存储。

Commit the changes immediately or with a separate commit while bulk processing multiple events. 立即或在批量处理多个事件时提交更改或单独提交。

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

Good luck and success. 祝你好运和成功。

first get the event using even Id then delete the event首先使用 even Id 获取事件然后删除事件

func removeEvent(eventId: String, eventStore: EKEventStore) {
        if let eventToDelete = self.eventStore.event(withIdentifier: eventId){
            do {
                try eventStore.remove(eventToDelete, span: .thisEvent)
            } catch let error as NSError {
                print("failed to save event with error : \(error)")
            }
            print("removed Event")
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM