This article is based on iOS 4 in Action, to be published on June 2011. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ Use promotional code ‘java40beat’ and get 40% discount on eBooks and pBooks ]
An Overview of the Event Kit Frameworks
Introduction
Calendar apps coming with iOS on iPhone and iPad are really convenient for several reasons. They allow users to check out the schedule on the go and consolidate all the information into one Calendar database.
Figure 1 Calendar app on iPhone and iPadIn order to access the Calendar database, Apple provides the convenient API in iOS 4 with event kit. There are two frameworks for the Event Kit, as we mentioned earlier. Event Kit framework gives you the access to insert and delete an event in the Calendar’s database. It’s a 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 a peaceful mind when you are writing the code for calendar access. Event Kit UI framework provides the handy interfaces to display and edit Calendar’s database with the same view controller you’ve already used with the Calendar app on the iPhone or iPad.
Adding Event Kit frameworks to your project
In order to use the Event Kit frameworks, we first need to add the existing Frameworks EventKit.framework and EventKitUI.framework to the project. Head over to Xcode, right-click on the folder Frameworks on left panel, choose Add>Existing Frameworks…; you should be able to see the whole list of available frameworks under current SDK, as shown in figure 2. Navigate to EventKit.framework and click on button Add. You will see a new framework added to your project. Repeat the same process for the Event Kit UI framework.
Figure 2 Add the Event Kit framework from the project panel.After adding the required frameworks to the project, you also need to include two files to the header file you wish to use, as below:
- #import <EventKit/EventKit.h>
- #import <EventKitUI/EventKitUI.h>
With the Event Kit frameworks added to the project, you can start to use them for accessing Calendar’s database from the application. First, we will take a look at the Event Kit Classes.
Event Kit Classes
Inside the powerful Event Kit API, there are a handful of classes that are like a lot of useful friends; figure 3 gives you a general idea about the relationships among these important classes.
Figure 3 The Event Kit Classes structureAs you can see from figure 3, EKEventStore is the key object here and it’s the connection to the calendar database. You can use the code snippet below to initialize an EKEventStore object for calendar data access.
EKEventStore *store = [[EKEventStore alloc] init];
Please note that this initial method may consume a lot of time, so it’s a good practice to keep that EKEventStore object in your program around for all the data access.
EKEvent is the object representing an event, which includes some import properties listed in table 1.
After the EKEventStore object is initialized, the calendar’s ready for adding or deleting events. You can create an event object and add it to the calendar’s database programmatically. Alternatively, you can use Event Kit UI framework for event view controllers, which is a great choice for user interface. The Event Kit UI framework contains two types of view controllers for manipulating events:
- EKEventViewController—Use the EKEventViewController class if you have an existing event you want to display or allow the user to edit.
- EKEventEditViewController—Use the EKEventEditViewController class if you allow the user to create, edit, or delete events.
Summary
The Event Kit framework provides an interface for accessing calendar events on a user’s device. You can use this framework to get existing events and add new events to the user’s calendar. In this article, you learned how to accomplish this task with the Event Kit UI view controllers.