How to Update or Edit EKEvent in iOS and store in native calendar using identifier?
You should save event identifier in database, so that you can later use it to update or delete the events. After creation of event using:
[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
You get access to event.eventIdentifier
. Save it in database. When you want to edit particular event, just get that event using the ID stored:
-(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];
}
}];
}
Hope it helps. Feel free to ask any query.