iOS

How to Add, Edit and Remove iOS Calendar Events

You can access a user calendar to insert new event, or to edit or delete.

Some code snippets:

Import EventKit

1
#import <EventKit/EventKit.h>

Add Event

1
2
3
4
5
6
7
8
9
10
11
12
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
  if (!granted) return;
  EKEvent *event = [EKEvent eventWithEventStore:store];
  event.title = @"Event Title";
  event.startDate = [NSDate date]; // today
  event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  // Duration 1 hr
  [event setCalendar:[store defaultCalendarForNewEvents]];
  NSError *err = nil;
  [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
  NSString *savedEventId = event.eventIdentifier;  // Store this so you can access this event later
}];

Edit Event

1
2
3
4
5
6
7
8
9
10
11
12
13
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
  if (!granted) return;
  EKEvent *event = [store eventWithIdentifier:savedEventId];
  // Uncomment below if you want to create a new event if savedEventId no longer exists
  // if (event == nil)
  //   event = [EKEvent eventWithEventStore:store];
  if (event) {
    NSError *err = nil;
    event.title = @"New event title";
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
  }
}];

Delete Event

1
2
3
4
5
6
7
8
9
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];
  }
}];

Comments