Friday, April 26, 2013

EKEventStore


- (BOOL)

createEventWithTitle:(NSString *)paramTitle startDate:(NSDate *)paramStartDate

endDate:(NSDate *)paramEndDate inCalendarWithTitle:(NSString *)paramCalendarTitle

inCalendarWithType:(EKCalendarType)paramCalendarType notes:(NSString *)paramNotes{

BOOL result = NO;
EKEventStore *eventStore = [[EKEventStore alloc] init];

/* Are there any calendars available to the event store? */

if ([eventStore.calendars count] == 0){ 706 | Chapter 16: Dates, Calendars, and Events

www.it-ebooks.info

NSLog(@"No calendars are found.");

return NO; }

EKCalendar *targetCalendar = nil;

/* Try to find the calendar that the user asked for */

for (EKCalendar *thisCalendar in eventStore.calendars){
if ([thisCalendar.title isEqualToString:paramCalendarTitle] &&

thisCalendar.type == paramCalendarType){ targetCalendar = thisCalendar;
break;

} }

/* Make sure we found the calendar that we were asked to find */

if (targetCalendar == nil){
NSLog(@"Could not find the requested calendar."); return NO;

}

/* If a calendar does not allow modification of its contents then we cannot insert an event into it */

if (targetCalendar.allowsContentModifications == NO){ NSLog(@"The selected calendar does not allow modifications."); return NO;

}

/* Create an event */

EKEvent *event = [EKEvent eventWithEventStore:eventStore]; event.calendar = targetCalendar;

/* Set the properties of the event such as its title, start date/time, end date/time, etc. */

event.title = paramTitle; event.notes = paramNotes; event.startDate = paramStartDate; event.endDate = paramEndDate;

/* Finally, save the event into the calendar */

NSError *saveError = nil;

result = [eventStore saveEvent:event span:EKSpanThisEvent

error:&saveError]; NSLog(@"An error occurred = %@", saveError);

if (result == NO){ }
return result

 

iOS syncs online calendars with the iOS calendar. These calendars could be Exchange, CalDAV, and other common formats. Creating an event on a CalDAV calendar on an iOS device will create the same event on the server. The server changes are also reflected in the iOS Calendar database when the Calendar database is synced with the server. 

 

- (EKCalendar *) calDAVCalendarWithTitleContaining :(NSString *)paramDescription{

EKCalendar *result = nil;

EKEventStore *eventStore = [[EKEventStore alloc] init];

for (EKCalendar *thisCalendar in eventStore.calendars){ if (thisCalendar.type == EKCalendarTypeCalDAV){

if ([thisCalendar.title rangeOfString:paramDescription].location != NSNotFound){

return thisCalendar; }

} }

return result; }

- (void) readEvents{

/* Find a calendar to base our search on */

EKCalendar *targetCalendar =
[self calDAVCalendarWithTitleContaining:@"gmail.com"];


/* If we could not find a CalDAV calendar that we were looking for, then we will abort the operation */

if (targetCalendar == nil){
NSLog(@"No CalDAV calendars were found.");

return; }

/* We have to pass an array of calendars to the event store to search */

NSArray *targetCalendars = [[NSArray alloc] initWithObjects: targetCalendar, nil];

/* Instantiate the event store */

EKEventStore *eventStore = [[EKEventStore alloc] init]; /* The start date will be today */

NSDate *startDate = [NSDate date];
/* The end date will be one day from today */

NSDate *endDate = [startDate dateByAddingTimeInterval:24 * 60 * 60];

/* Create the predicate that we can later pass to the event store in order to fetch the events */

NSPredicate *searchPredicate =
[eventStore predicateForEventsWithStartDate:startDate

endDate:endDate calendars:targetCalendars];

/* Make sure we succeeded in creating the predicate */

if (searchPredicate == nil){
NSLog(@"Could not create the search predicate."); return;

}

/* Fetch all the events that fall between the starting and the ending dates */

NSArray *events = [eventStore eventsMatchingPredicate:searchPredicate];

/* Go through all the events and print their information out to the console */

if (events != nil){ NSUInteger counter = 1;

for (EKEvent *event in events){

NSLog(@"Event %lu Start Date = %@", (unsigned long)counter,

event.startDate);

NSLog(@"Event %lu End Date = %@", (unsigned long)counter,

event.endDate); NSLog(@"Event %lu Title = %@",

(unsigned long)counter,

event.title); counter++;

}

} else {
NSLog(@"The array of events for this start/end time is nil.");

} }

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

[self readEvents];
self.window = [[UIWindow alloc] initWithFrame:

[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible];
return YES;

}