Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@martinsik
Created March 8, 2013 09:47
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save martinsik/5115383 to your computer and use it in GitHub Desktop.
Save martinsik/5115383 to your computer and use it in GitHub Desktop.
Example of using EventKit on iOS 6+
#import <Foundation/Foundation.h>
@interface MyCalendar : NSObject
+ (void)requestAccess:(void (^)(BOOL granted, NSError *error))success;
+ (BOOL)addEventAt:(NSDate*)eventDate withTitle:(NSString*)title inLocation:(NSString*)location;
@end
#import "MyCalendar.h"
#import <EventKit/EventKit.h>
static EKEventStore *eventStore = nil;
@implementation MyCalendar
+ (void)requestAccess:(void (^)(BOOL granted, NSError *error))callback;
{
if (eventStore == nil) {
eventStore = [[EKEventStore alloc] init];
}
// request permissions
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:callback];
}
+ (BOOL)addEventAt:(NSDate*)eventDate withTitle:(NSString*)title inLocation:(NSString*)location
{
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
EKCalendar *calendar = nil;
NSString *calendarIdentifier = [[NSUserDefaults standardUserDefaults] valueForKey:@"my_calendar_identifier"];
// when identifier exists, my calendar probably already exists
// note that user can delete my calendar. In that case I have to create it again.
if (calendarIdentifier) {
calendar = [eventStore calendarWithIdentifier:calendarIdentifier];
}
// calendar doesn't exist, create it and save it's identifier
if (!calendar) {
// http://stackoverflow.com/questions/7945537/add-a-new-calendar-to-an-ekeventstore-with-eventkit
calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:eventStore];
// set calendar name. This is what users will see in their Calendar app
[calendar setTitle:[KKCalendar calendarName]];
// find appropriate source type. I'm interested only in local calendars but
// there are also calendars in iCloud, MS Exchange, ...
// look for EKSourceType in manual for more options
for (EKSource *s in eventStore.sources) {
if (s.sourceType == EKSourceTypeLocal) {
calendar.source = s;
break;
}
}
// save this in NSUserDefaults data for retrieval later
NSString *calendarIdentifier = [calendar calendarIdentifier];
NSError *error = nil;
BOOL saved = [eventStore saveCalendar:calendar commit:YES error:&error];
if (saved) {
// http://stackoverflow.com/questions/1731530/whats-the-easiest-way-to-persist-data-in-an-iphone-app
// saved successfuly, store it's identifier in NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:calendarIdentifier forKey:@"my_calendar_identifier"];
} else {
// unable to save calendar
return NO;
}
}
// this shouldn't happen
if (!calendar) {
return NO;
}
// assign basic information to the event; location is optional
event.calendar = calendar;
event.location = location;
event.title = title;
// set the start date to the current date/time and the event duration to two hours
NSDate *startDate = eventDate;
event.startDate = startDate;
event.endDate = [startDate dateByAddingTimeInterval:3600 * 2];
NSError *error = nil;
// save event to the callendar
BOOL result = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
if (result) {
return YES;
} else {
// NSLog(@"Error saving event: %@", error);
// unable to save event to the calendar
return NO;
}
}
@end
[MyCalendar requestAccess:^(BOOL granted, NSError *error) {
if (granted) {
BOOL result = [MyCalendar addEventAt:[NSDate date] withTitle:@"Party" inLocation:@"My house"];
if (result) {
// added to calendar
} else {
// unable to create event/calendar
}
} else {
// you don't have permissions to access calendars
}
}];
@vexdev
Copy link

vexdev commented Mar 5, 2014

Really cool, thank you!
Just a side note: on line 35 of the implementation you use this:

[KKCalendar calendarName]

i guess that's something specific to your app. Greate code anyway!

@sansuba
Copy link

sansuba commented Oct 29, 2014

Thank you so much, you saved my time lot.

@kevinylu
Copy link

kevinylu commented Mar 3, 2015

It will better to add [[NSUserDefaults standardUserDefaults] synchronize]; after [[NSUserDefaults standardUserDefaults] setObject:calendarIdentifier forKey:@"my_calendar_identifier"];

@sreeji44
Copy link

is their anyway to get the swift code pls

@Panway
Copy link

Panway commented Nov 16, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment