An overview of the Event Kit frameworks (iOS 4)

With the Calendar app in iOS 4, iPhone or iPad owners can easily consolidate their Gmail account, Microsoft Exchange account, Mobile Me account, and other calendar accounts on the same device. The Calendar’s database can be accessed within your application through Event Kit frameworks. The Event Kit frameworks are made up of two frameworks: the Event Kit framework and the Event Kit UI framework. Together, they help your application access the Calendar’s database from a high level. Previously, in topic 9, we covered the Address Book frameworks on iOS. The Event Kit frameworks are quite similar to the Address Book API in a way.

Imagine you can build a birthday event planner application and have every friend’s birthday party plan automatically added to the Calendar’s database by clicking one button. In this topic, we’ll first provide some basic references on the Event Kit framework with the Birthday application, and then we’ll show another Event application to fetch and display the existing events from the Calendar’s database to the table view controller, with the help of the Event Kit UI framework. Finally, with the help of Grand Central Dispatch (GCD) from iOS 4, we’ll demonstrate an improved technique for fetching data from Calendar’s database.

Calendar apps coming with iOS on iPhone and iPad are convenient for several reasons (see figure 16.1). They allow users to check out their schedule on the go and consolidate all the information into one Calendar database.


In order to access the Calendar database, Apple provides a convenient API in iOS 4 called Event Kit. There are two frameworks for Event Kit, as we mentioned earlier. The Event Kit framework gives you access to insert and delete an event in the Calendar’s database. It’s high-level API access to the Calendar database, and the best part is that any changes made to the Calendar will be synced automatically, so you can have peace of mind when you’re writing the code for Calendar access. The Event Kit UI framework provides the handy interfaces to display and edit the Calendar’s database with the same view controller you’re already familiar with by using the Calendar app on the iPhone or iPad.

Adding Event Kit frameworks to your project

In order to use the Event Kit frameworks, you first need to add the existing frameworks EventKit.framework and EventKitUI.framework into the project. Head over to Xcode, highlight the top-level Project node in the project navigator tree, and on the right side view choose a target; then select the Build Phases tab. Under this tab is an entry called Link Binary with Libraries.

Calendar app on iPhone and iPad

Figure 16.1 Calendar app on iPhone and iPad

Click the + button, and you’ll see a window with the entire list of available frameworks under the current SDK, as shown in figure 16.2. Navigate to EventKit. framework and click the Add button. You’ll see a new framework added to your project. Repeat the same process for EventKitUI.framework.

After adding the required frameworks into the project, you also need to include two files to the header file you wish to use Event Kit, as follows:

#import <EventKit/EventKit.h> #import <EventKitUI/EventKitUI.h>

With the Event Kit frameworks added into the project, you can start to use them for accessing Calendar’s database from the application. First, we’ll look at the Event Kit classes.

Add the Event Kit framework from the project panel.

Figure 16.2 Add the Event Kit framework from the project panel.

Event Kit classes

Inside the powerful Event Kit API are a handful of classes that are like useful friends; figure 16.3 gives you a general idea about the relationships among these important classes.

As you can see from figure 16.3, EKEventStore is the key object here, and it’s the connection to the Calendar database. You can use the following code snippet to initialize an EKEventStore object for Calendar data access:

tmp5A202_thumb

Note that this initial method may consume a lot of time, so it’s a good practice to keep around that EKEventStore object in your program for all data access.

The Event Kit class structure

Figure 16.3 The Event Kit class structure

EKEvent is the object representing an event, which includes some import properties, as listed in table 16.1.

Table 16.1 EKEvent’s property table

Property

Details

title

Title of the event; NSString type

location

Location of the event; NSString type

allday

bool type, indicating the event is an all-day event

startDate

Start date of the event

endDate

End date of the event

calendar

Calendar for a new event, EKCalendar type

attendees

Array of participants

alarms

Array of EKAlarm objects

eventIdentifier

Unique identifier for an event; NSString type

After the EKEventStore object is initialized, the Calendar is ready for you to add or delete events. You can create an event object and add it to the Calendar’s database programmatically. Alternatively, you can use the Event Kit UI framework for event view controllers, which is a great choice for calendaring related user interaction. The Event Kit UI framework contains two types of view controllers for manipulating events:

■ EKEventViewController—Use if you have an existing event you want to display or allow the user to edit

■ EKEventEditViewController—Use if you allow the user to create, edit, or delete events

We’ll talk about how to do this in section 16.3. Next, let’s look at how to add a new event to Calendar programmatically with the Event Kit framework.

Next post:

Previous post: