Created
March 8, 2013 09:47
-
-
Save martinsik/5115383 to your computer and use it in GitHub Desktop.
Example of using EventKit on iOS 6+
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 | |
} | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good wrapper:
https://github.com/jingsh/JSCalendarManager