通过使用EventKit,我可以将事件添加到iphone日历,我可以使用EkCalendar制作新的日历。但现在需要删除iphone日历中的单个事件。我怎样才能做到这一点。提前致谢
func retrieveYourCalendar() {
let calendars = eventStore.calendarsForEntityType(EKEntityType.Event)
if(yourReminderCalendar == nil) {
for calendar in calendars {
if calendar.title == "new" {
yourReminderCalendar = (calendar as EKCalendar)
// Exist = true
break
}
}
if(yourReminderCalendar == nil) {
yourReminderCalendar = EKCalendar(forEntityType: EKEntityType.Event, eventStore: eventStore)
yourReminderCalendar!.title = "new"
yourReminderCalendar!.source = eventStore.defaultCalendarForNewEvents.source
// var error: NSError?
do {
try eventStore.saveCalendar(yourReminderCalendar!, commit: true)
// self.calendarIdentifier = yourReminderCalendar!.calendarIdentifier
print("Event calender saved")
}
catch(_){}
}
}
}
答案 0 :(得分:1)
在向日历添加事件时,将 eventIdentifier 存储在数据库或任何存储中。识别要删除的特定事件将非常有用。
通过使用以下代码,您可以根据标识符
删除事件目标C:
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (!granted) return;
EKEvent* eventToRemove = [store eventWithIdentifier:savedEventId];
if (eventToRemove) {
NSError* err = nil;
[store removeEvent:eventToRemove span:EKSpanThisEvent commit:YES error:&err];
}
}];
Swift:根据开始日期和结束日期提取事件。
// Create an Event Store instance
let eventStore = EKEventStore();
eventStore.requestAccessToEntityType(EKEntityType.Event, completion: {
(accessGranted: Bool, error: NSError?) in
if accessGranted == true {
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 {
eventStore.removeEvent(eV, span:EKSpanFutureEvents, commit:true, error:&error)
}
} else {
return
}
})
检查swift的更新答案。 仅供参考:我没有测试swift的代码。