我在我的应用程序中使用EKEvent来获取所有商店事件,我想通过标识符编辑任何特定事件并在现有文件中重新保存。那我该怎么做呢?
EKEventStore *store = [EKEventStore new];
EKEvent *event = [EKEvent eventWithEventStore:store];
event.title = @"abc";
event.notes= @"this is updated notes";
event.calendar = [store defaultCalendarForNewEvents];
NSError *err = nil;
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
此代码适用于我用来存储事件,但是在哪里传递标识符仅用于更新特定事件?
答案 0 :(得分:2)
您应该将事件标识符保存在数据库中,以便以后可以使用它来更新或删除事件。在创建事件后使用:
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
您可以访问event.eventIdentifier
。将其保存在数据库中。如果要编辑特定事件,只需使用存储的ID获取该事件:
-(void)updateNotification:(NSMutableDictionary *) info
{
EKEventStore *eventStore = [[EKEventStore alloc] init];
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (!granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot access Calendar" message:@"Please give the permission to add task in calendar from iOS > Settings > Privacy > Calendars" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
});
return;
}
if (error)
{
NSLog(@"%@", error);
}
//this is event ID you saved in DB. now you want to edit that event
EKEvent *event = [eventStore eventWithIdentifier:info[@"eventID"]];
if(event) {
//You have your valid event. Modify as per your need.
event.title = [info valueForKey:@"title"];
event.notes = [info valueForKey:@"description"];
event.startDate = [info objectForKey:@"date"];
event.endDate = [[NSDate alloc] initWithTimeInterval:3600*24 sinceDate:event.startDate];
event.calendar = eventStore.defaultCalendarForNewEvents;
[eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
}
}];
}
希望它有所帮助。随意询问任何问题。
答案 1 :(得分:0)
这是获取所有事件的代码。参考。 Apple演示项目
- (NSMutableArray *)fetchEvents{
NSDate *startDate = [NSDate date];
//Create the end date components
NSDateComponents *tomorrowDateComponents = [[NSDateComponents alloc] init];
tomorrowDateComponents.day = 1;
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:tomorrowDateComponents
toDate:startDate
options:0];
// We will only search the default calendar for our events
NSArray *calendarArray = @[self.defaultCalendar];
// Create the predicate
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:calendarArray];
// Fetch all events that match the predicate
NSMutableArray *events = [NSMutableArray arrayWithArray:[self.eventStore eventsMatchingPredicate:predicate]];
return events;
}
参考演示项目: - > EkEvent demo
请参阅此 - > Document
答案 2 :(得分:-1)
func editOneEventToCalendar(id:String,title: String, description: String?,alarams :[EKAlarm],location:String,contacts:[EKParticipant],repeatString :String,timeZone:NSTimeZone, startDate: NSDate, endDate: NSDate , isAllDay :Bool ,calendar:EKCalendar,StruLocation:EKStructuredLocation?) -> Bool{
let eventStore = EventsManager.getEventStore()
let event = EventsManager.geteventID(id: id)
event.title = title
event.startDate = startDate as Date
event.endDate = endDate as Date
event.notes = description
event.calendar = calendar
event.timeZone = timeZone as TimeZone //NSTimeZone() as TimeZone //timeZone as TimeZone
if StruLocation != nil {
event.structuredLocation = StruLocation
}else {
event.structuredLocation = StruLocation
}
event.isAllDay = isAllDay
if repeatString != "None" {
let rule = EventsManager.getRepeatValue(repeatString)//self.getRepeatValue(option: repeat)
event.addRecurrenceRule(rule!)
}else if repeatString == "None"{
if event.hasRecurrenceRules {
let rrrl = event.recurrenceRules
for i in rrrl! {
event.removeRecurrenceRule(i)
}
}
}
for item in alarams {
event.addAlarm(item)
}
// let formatter = CNContactFormatter()
// formatter.style =
var attendees = [EKParticipant]()
// for item in contacts {
//
// let con = event.(contact: item)
// attendees.append(con!)
//
// }
do {
try eventStore.save(event, span: .thisEvent, commit: true)
//try eventStore.save(event, span: .thisEvent)
// if saved edit the local db ??
return true
} catch let e as NSError {
EZAlertController.alert(e.debugDescription)
}
return false
}
func editFutureEventToCalendar(id:String,title: String, description: String?,alarams :[EKAlarm],location:String,contacts:[EKParticipant],repeatString :String,timeZone:NSTimeZone, startDate: NSDate, endDate: NSDate , isAllDay :Bool,calendar:EKCalendar,StruLocation:EKStructuredLocation?)->Bool {
let eventStore = EventsManager.getEventStore()
let event = EventsManager.geteventID(id: id)
let ocdate = event.occurrenceDate
event.title = title
event.startDate = startDate as Date
event.endDate = endDate as Date
event.notes = description
event.calendar = calendar
// event.occurrenceDate = ocdate
event.timeZone = timeZone as TimeZone //timezoneNSTimeZone() as TimeZone //timeZone as TimeZone
event.isAllDay = isAllDay
if StruLocation != nil {
event.structuredLocation = StruLocation
}else {
event.structuredLocation = StruLocation
}
if repeatString != "None" {
if event.hasRecurrenceRules {
let rrrl = event.recurrenceRules
for i in rrrl! {
event.removeRecurrenceRule(i)
}
}
let rule = EventsManager.getRepeatValue(repeatString)//self.getRepeatValue(option: repeat)
event.addRecurrenceRule(rule!)
}else if repeatString == "None"{
if event.hasRecurrenceRules {
let rrrl = event.recurrenceRules
for i in rrrl! {
event.removeRecurrenceRule(i)
}
}
}
for item in alarams {
event.addAlarm(item)
}
// event.location = location
// let formatter = CNContactFormatter()
// formatter.style =
var attendees = [EKParticipant]()
// for item in contacts {
//
// let con = event.(contact: item)
// attendees.append(con!)
//
// }
do {
try eventStore.save(event, span: .futureEvents, commit: true)
//try eventStore.save(event, span: .thisEvent)
return true
} catch let e as NSError {
EZAlertController.alert(e.debugDescription)
}
return false
}