iOS Reference Library Apple Developer
Search

Creating and Editing Events Programmatically

You can use the Event Kit framework to allow users to create new events and edit existing events in their Calendar database.

Note: The recommended method for allowing users to modify event data is with the event view controllers provided in the Event Kit UI framework. For information on how to use these event view controllers, see ‚ÄúUsing Event View Controllers.‚Äù Use the techniques described in this chapter only if event view controllers are not appropriate for your application.

Always Notify the User

If your application modifies a user’s Calendar database programmatically, it must get confirmation from the user before doing so. An application should never modify the Calendar database without specific instruction from the user.

Creating and Editing Events

Create a new event with the eventWithEventStore: method of the EKEvent class.

To edit the details of a new event or an event you have fetched from the Calendar database, set the corresponding properties of the event. The details you can edit include:

Adding and Removing Alarms

You can add an alarm to an event with the addAlarm: method. Alarms can be created with an absolute date or with an offset relative to the start date of the event. Alarms created with a relative offset must occur before or at the start date of the event. You can remove an alarm from an event with the removeAlarm: method.

Saving Events

Changes you make to an event are not permanent until you save them. Save your changes to the Calendar database with the EKEventStore method saveEvent:span:error:. Doing so automatically syncs your changes with the calendar the event belongs to (CalDAV, Exchange, and so on).

If you are saving a recurring event, you can have your changes apply to all occurrences of the event by specifying EKSpanFutureEvents for the span parameter of the saveEvent:span:error: method.

Removing Events

Permanently remove an event from the Calendar database with the EKEventStore method removeEvent:span:error:.

If you are removing a recurring event, you can remove all occurrences of the event by specifying EKSpanFutureEvents for the span parameter of the removeEvent:span:error: method.

Processing Events with a Predicate

You can perform an operation on all events that match a provided predicate with the EKEventStore method enumerateEventsMatchingPredicate:usingBlock:. You must create the predicate for this method with the EKEventStore method predicateForEventsWithStartDate:endDate:calendars:. The operation you provide is a block of type EKEventSearchCallback.

typedef void (^EKEventSearchCallback)(EKEvent *event, BOOL *stop);

The block is passed two parameters:

event

This is the event that is currently being operated on.

stop

You can set the value of this parameter to YES to tell the enumerateEventsMatchingPredicate:usingBlock: method to stop processing events when this block returns. Any events that match the predicate but have not yet been processed remain unprocessed.

Keep in mind that using this method can result in significant changes to the user’s Calendar database. Make sure the user is fully informed of the actions you are about to perform when you request user confirmation.

Because the enumerateEventsMatchingPredicate:usingBlock: method is synchronous, you may not want to run it on your application’s main thread. For asynchronous behavior, run the method on another thread with the dispatch_async function or with an NSOperation object.




Last updated: 2010-08-03

Did this document help you? Yes It's good, but... Not helpful...