SimpleEKDemo is an example sample code project that utilizes events in Event Kit.
不论是 Events 还是remainder 都是存储在 calendar database中,统一用类EKEventStore检索,
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
|
// handle access here
|
}]; |
Fetching events with a predicate
// Get the appropriate calendar |
NSCalendar *calendar = [NSCalendar currentCalendar]; |
|
// Create the start date components |
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init]; |
oneDayAgoComponents.day = -1; |
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents |
toDate:[NSDate date] |
options:0]; |
|
// Create the end date components |
NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init]; |
oneYearFromNowComponents.year = 1; |
NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents |
toDate:[NSDate date] |
options:0]; |
|
// Create the predicate from the event store's instance method |
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo |
endDate:oneYearFromNow |
calendars:nil]; |
|
// Fetch all events that match the predicate |
NSArray *events = [store eventsMatchingPredicate:predicate]; |
Retrieving Reminders
You can call fetchRemindersMatchingPredicate:completion:
to access multiple reminders that match a predicate. Pass a predicate returned by one of the following methods:
-
predicateForIncompleteRemindersWithDueDateStarting:ending:calendars:
finds incomplete reminders within an optional time period -
predicateForCompletedRemindersWithCompletionDateStarting:ending:calendars:
finds completed reminders within an optional time period -
predicateForRemindersInCalendars:
finds all reminders
NSPredicate *predicate = [store predicateForRemindersInCalendars:nil]; |
|
[store fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) { |
for (EKReminder *reminder in reminders) { |
// do something for each reminder |
} |
}]; |
calendarItemWithIdentifier:
can fetch any calendar item (reminders and events), whereas eventWithIdentifier:
fetches only events.
Observing External Changes to the Calendar Database
[[NSNotificationCenter defaultCenter] addObserver:self |
selector:@selector(storeChanged:) |
name:EKEventStoreChangedNotification |
object:eventStore]; |
The Event Kit UI framework provides two types of view controllers for manipulating events:
-
EKEventViewController
: use this class if you have an existing event you want to display. -
EKEventEditViewController
: use this class to allow the user to create, edit, or delete events.
EKEventViewController *eventViewController = [[EKEventViewController alloc] init]; |
eventViewController.event = myEvent; |
eventViewController.allowsEditing = YES; |
navigationController = [[UINavigationController alloc] initWithRootViewController:eventViewController]; |
EKEventEditViewController* controller = [[EKEventEditViewController alloc] init]; |
controller.eventStore = myEventStore; |
controller.editViewDelegate = self; |
[self presentModalViewController:controller animated:YES]; |
- (void)eventEditViewController:(EKEventEditViewController *)controller
didCompleteWithAction:(EKEventEditViewAction)action {
NSError *error = nil;
EKEvent *thisEvent = controller.event;
switch (action) {
case EKEventEditViewActionCanceled:
// Edit action canceled, do nothing.
break;
case EKEventEditViewActionSaved:
// When user hit "Done" button, save the newly created event to the event store,
// and reload table view.
// If the new event is being added to the default calendar, then update its
// eventsList.
if (self.defaultCalendar == thisEvent.calendar) {
[self.eventsList addObject:thisEvent];
}
[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];
[self.tableView reloadData];
break;
case EKEventEditViewActionDeleted:
// When deleting an event, remove the event from the event store,
// and reload table view.
// If deleting an event from the currenly default calendar, then update its
// eventsList.
if (self.defaultCalendar == thisEvent.calendar) {
[self.eventsList removeObject:thisEvent];
}
[controller.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:&error];
[self.tableView reloadData];
break;
default:
break;
}
// Dismiss the modal view controller
[controller dismissModalViewControllerAnimated:YES];
}