Reputation: 399
Is there a command that can remove all the events from a calendar in Swift?
Upvotes: 1
Views: 4510
Reputation: 3289
Swift 3 compatible:
func removeAllEventsMatchingPredicate() {
let startDate = NSDate().addingTimeInterval(60*60*24*(-2))
let endDate = NSDate().addingTimeInterval(60*60*24*7)
let predicate2 = eventStore.predicateForEvents(withStart: startDate as Date, end: endDate as Date, calendars: nil)
print("startDate:\(startDate) endDate:\(endDate)")
let eV = eventStore.events(matching: predicate2) as [EKEvent]!
if eV != nil {
for i in eV! {
do{
(try eventStore.remove(i, span: EKSpan.thisEvent, commit: true))
}
catch let error {
print("Error removing events: ", error)
}
}
}
}
Upvotes: 5
Reputation: 1133
Considering you are saving the events in with startDate
,endDate
,description
and a title
var event:EKEvent = EKEvent(eventStore: eventStore)
event.title = "Test Title"
event.startDate = NSDate()
event.endDate = NSDate()
event.notes = "This is a note"
event.calendar = eventStore.defaultCalendarForNewEvents
eventStore.saveEvent(event, span: EKSpanThisEvent, error: nil)
Then all you need is to do is delete the event as this:
var startDate=NSDate().dateByAddingTimeInterval(-60*60*24)
var endDate=NSDate().dateByAddingTimeInterval(60*60*24*3)
var predicate2 = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
println("startDate:\(startDate) endDate:\(endDate)")
var eV = eventStore.eventsMatchingPredicate(predicate2) as [EKEvent]!
if eV != nil {
for i in eV {
println("Title \(i.title)" )
println("stareDate: \(i.startDate)" )
println("endDate: \(i.endDate)" )
do{
(try eventStore.removeEvent(i, span: EKSpan.ThisEvent, commit: true))
}
catch let error {
}
}
}
}
Upvotes: 10