【问题标题】:how to remove certain / Specific event using EventKit如何使用 EventKit 删除某些/特定事件
【发布时间】:2018-06-15 14:13:51
【问题描述】:

我需要删除某个/特定标题的事件,我希望我可以根据 eventID/Identifier 删除/删除该事件。但我不知道如何在代码中做到这一点。我不知道如何为事件提供标识符并根据其标识符/标题将其删除。

这是我用来保存事件的代码:

let eventStore = EKEventStore()
        let newEvent = EKEvent(eventStore: eventStore)

        newEvent.calendar = eventStore.defaultCalendarForNewEvents
        newEvent.title = self.eventNameTextField.text ?? "Some Event Name"
        newEvent.startDate = timeDatePicker.date
        newEvent.endDate = endTimeDatePicker.date
        newEvent.notes = "Ini adalah catatan"
        newEvent.location = "Jalan Sunda kelapa no.60"

        let eventAlarm = EKAlarm(relativeOffset: -60 * 10) // 10 minutes before the start date
        newEvent.alarms = [eventAlarm]


        do {
            try eventStore.save(newEvent, span: .thisEvent)
            print("Event has been saved")
        } catch {
            let alert = UIAlertController(title: "Event could not be saved", message: (error as NSError).localizedDescription, preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alert.addAction(OKAction)

            self.present(alert, animated: true, completion: nil)
        }

我知道我可以使用 evenStore.remove() ,但该方法需要 EKEvent 实例。如果使用该方法,我不明白如何删除特定事件,如果我可以根据其标识符删除事件会更容易

【问题讨论】:

    标签: ios swift eventkit ekeventkit


    【解决方案1】:

    实际上,EKEvent 实例有一个名为 eventIdentifier 的仅获取属性。您无法修改此标识符,但您可以在保存活动后获取它。所以:

        do {
            try eventStore.save(newEvent, span: .thisEvent)
            let id = newEvent.eventIdentifier ?? "NO ID"
            //Save your ID in your database or anywhere else so you can retrieve the event later
            print("Event has been saved with id \(id)")
        } catch {
            let alert = UIAlertController(title: "Event could not be saved", message: (error as NSError).localizedDescription, preferredStyle: .alert)
            let OKAction = UIAlertAction(title: "OK", style: .default, handler: nil)
            alert.addAction(OKAction)
    
            self.present(alert, animated: true, completion: nil)
        }
    

    然后你可以使用它的标识符来获取事件

    let event = eventStore.event(withIdentifier: id)
    

    然后将这个EKEvent 传递给eventStore.remove()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 2020-07-19
      • 1970-01-01
      相关资源
      最近更新 更多