Event Kit Programming Guide



Similar documents
TestFlight FAQ Apple Inc.

Apple Certificate Library Functional Specification

itunes Connect App Analytics Guide v1

WiFiSurvey Using AirPort Utility for WiFi Scanning Guide

Networking & Internet

Synology SSO Server. Development Guide

Mail Programming Topics

Synology NAS Server Windows ADS FAQ

ios Team Administration Guide (Legacy)

Apple Applications > Safari

Synology NAS Server Mail Station User Guide

Object Level Authentication

WiFiPerf User Guide 1.5

Creating Carbon Menus. (Legacy)

Synology NAS Server. Group Installation Guide Synology NAS Server Group Installation Guide

StarterPak: HubSpot and Dynamics CRM Lead and Contact Synchronization

Introduction to Version Control in

Apple URL Scheme Reference

Dell Spotlight on Active Directory Server Health Wizard Configuration Guide

Dell One Identity Cloud Access Manager How to Configure vworkspace Integration

Xcode User Default Reference. (Legacy)

Apple Cryptographic Service Provider Functional Specification

MiSync Personal for Beams

Synology DiskStation

formerly Help Desk Authority HDAccess Administrator Guide

Security Analytics Engine 1.0. Help Desk User Guide

Quick Connect Express for Active Directory

Software Hosting and End-User License Subscription Agreement

Dell Statistica Document Management System (SDMS) Installation Instructions

Dell Migration Manager for Enterprise Social What Can and Cannot Be Migrated

Self Help Guides. Setup Exchange with Outlook

Xcode Source Management Guide. (Legacy)

Symantec Enterprise Vault

AvePoint Record Rollback for Microsoft Dynamics CRM

AvePoint Tags 1.1 for Microsoft Dynamics CRM. Installation and Configuration Guide

Android App User Guide

How to Quickly Create Custom Applications in SharePoint 2010 or 2013 without Custom Code

Big Brother Professional Edition Windows Client Getting Started Guide. Version 4.60

FOR WINDOWS FILE SERVERS

Microsoft Dynamics GP. Field Service - Preventive Maintenance

CENTRAL SAVINGS BANK BUSINESS INTERNET BANKING AGREEMENT

Symantec AntiVirus Corporate Edition Patch Update

ecatholic Payments Terms of Service Agreement

Symantec Enterprise Vault

formerly Help Desk Authority Quest Free Network Tools User Manual

Dell InTrust Preparing for Auditing Cisco PIX Firewall

Symantec Enterprise Vault

Terms and Conditions. Wisconsin Department of Safety and Professional Services Application Hosting Agreement

Dell Statistica Statistica Enterprise Installation Instructions

BlackBerry Desktop Manager Version: User Guide

AeroScout Industrial Support Policy

WebEx Integration to Outlook. User Guide

StudentInformationSystem

CIFS/NFS Gateway Product Release Notes. Version May 2015 Revision A0

AvePoint Timeline Pro for Microsoft Dynamics CRM. Installation and Configuration Guide

Dell Enterprise Reporter 2.5. Configuration Manager User Guide

Spotlight Management Pack for SCOM

Authorize.Net Mobile Application

Getting Started with Apple Pay on the Authorize.Net Platform

online banking, billpay & electronic services agreement

Symantec Enterprise Vault

Cisco TelePresence Management Suite Extension for Microsoft Exchange Version 4.0

Symantec Enterprise Vault

STANLEY HEALTHCARE SUPPORT POLICY

Partners in Care Welch Allyn Connex Software Development Kit License Agreement

Symantec Enterprise Vault

Seagate Media Sync User Guide

Symantec Enterprise Vault

AGREEMENT BETWEEN USER AND Global Clinical Research Management, Inc.

Cisco TelePresence Management Suite Extension for Microsoft Exchange Version 4.0.3

Computer Information Concepts. CIC Data Health Check 3.0

Acquia Certification Program Agreement

BES10 Self-Service. Version: User Guide

Dell InTrust Preparing for Auditing Microsoft SQL Server

New Features and Enhancements

Application Menu and Pop-up List Programming Topics

Self Help Guides. Create a New User in a Domain

Simba ODBC Driver with SQL Connector for Apache Cassandra

Terms & Conditions Template

TOOLS for CC121 Installation Guide

Copy Tool For Dynamics CRM 2013

Open EMS Suite. O&M Agent. Functional Overview Version 1.2. Nokia Siemens Networks 1 (18)

How To Protect Your Active Directory (Ad) From A Security Breach

AGREEMENT BETWEEN USER AND International Network of Spinal Cord Injury Nurses

Online Banking Client Agreement and Disclosure Statement

Mobile Time Manager. Release 1.2.1

Central Management System (CMS) USER MANUAL

The Credit Control, LLC Web Site is comprised of various Web pages operated by Credit Control, LLC.

CONSULTING SERVICES AGREEMENT

Gateway Reporting Developer Guide

Card Account means your Card account that is in relation to your Visa Wallet maintained and operated by Tune Money Sdn Bhd.

NEXT Analytics User Guide for Facebook

i2 Virtual Office T&Cs

Dell One Identity Manager 7.0. Help Desk Module Administration Guide

Zander Identity Theft Solutions Benefits Summary and Terms of Service

Dell One Identity Quick Connect for Cloud Services 3.6.0

Transcription:

Event Kit Programming Guide

Contents Introduction 4 Who Should Read This Document? 4 Organization of This Document 4 See Also 4 Fetching Events 6 Initializing an Event Store 6 Fetching Events with a Predicate 6 Fetching Individual Events with an Identifier 8 Sorting Events by Start Date 8 Using Event View Controllers 9 Displaying and Editing Events 9 Creating and Editing Events 10 Creating and Editing Events Programmatically 12 Always Notify the User 12 Creating and Editing Events 12 Adding and Removing Alarms 13 Saving Events 13 Removing Events 13 Processing Events with a Predicate 13 Creating Recurring Events 15 Creating a Basic Recurrence Rule 15 Creating a Complex Recurrence Rule 15 Observing Event Changes 17 Observing Notifications 17 Responding to Notifications 17 Document Revision History 19 2

Listings Fetching Events 6 Listing 1-1 Fetching events with a predicate 6 Using Event View Controllers 9 Listing 2-1 Editing an existing event 9 Listing 2-2 Presenting an event edit view controller modally 10 Listing 2-3 The delegate dismisses the modal view 10 3

Introduction The Event Kit and Event Kit UI frameworks together allow ios applications to access event information from a user s Calendar database. You can fetch events based on a date range or a unique identifier, receive notifications when event records change, and allow users to create and edit events for any of their calendars. Changes made to events in a user s Calendar database with Event Kit are automatically synced with the appropriate calendar (CalDAV, Exchange, and so on). This document describes Event Kit concepts and common programming tasks. Who Should Read This Document? You should read this document if you want to display calendar event data or allow users to edit their calendar event data in your ios application. Event Kit provides limited access to a user s calendar information. It is not suitable for implementing a full-featured calendar application. Organization of This Document This document contains the following information: Fetching Events (page 6) explains how to fetch events from the Calendar database. Using Event View Controllers (page 9) explains how to display event view controllers to allow your users to create and edit events. Creating and Editing Events Programmatically (page 12) explains how to create and edit events programmatically. Creating Recurring Events (page 15) explains how to make an event a recurring event. Observing Event Changes (page 17) explains how to register for notifications about external changes to the Calendar database. See Also For an in-depth description of the Event Kit and Event Kit UI API, read: Event Kit Framework Reference provides an in-depth description of the Event Kit API. 4

Introduction See Also Event Kit UI Framework Reference provides an in-depth description of the Event Kit UI API. The SimpleEKDemo sample code provides a basic example of using the Event Kit and Event Kit UI frameworks to access and edit calendar data. 5

Fetching Events You can fetch events from a user s Calendar database using the EKEventStore class. You can fetch a custom set of events that match a predicate you provide, or you can fetch an individual event by its unique identifier. After you fetch an event, you can access its associated calendar information with the properties of the EKEvent class. Initializing an Event Store You initialize an EKEventStore object with the default initializer: EKEventStore *store = [[EKEventStore alloc] init]; An EKEventStore object requires a relatively large amount of time to initialize and release. Consequently, you should not initialize and release a separate event store for each event-related task. Instead, initialize a single event store when your app loads and use it repeatedly. Fetching Events with a Predicate It s common to fetch events that fall within a date range. The event store method eventsmatchingpredicate: fetches all events that fall within the date range specified in the predicate you provide. You must create the predicate for the eventsmatchingpredicate: method with the EKEventStore method predicateforeventswithstartdate:enddate:calendars:. Listing 1-1 demonstrates fetching all events that occur between 30 days before the current date and 15 days after the current date. Listing 1-1 Fetching events with a predicate // Create the predicate's start and end dates. CFGregorianDate gregorianstartdate, gregorianenddate; CFGregorianUnits startunits = {0, 0, -30, 0, 0, 0}; CFGregorianUnits endunits = {0, 0, 15, 0, 0, 0}; CFTimeZoneRef timezone = CFTimeZoneCopySystem(); 6

Fetching Events Fetching Events with a Predicate gregorianstartdate = CFAbsoluteTimeGetGregorianDate( CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), timezone, startunits), timezone); gregorianstartdate.hour = 0; gregorianstartdate.minute = 0; gregorianstartdate.second = 0; gregorianenddate = CFAbsoluteTimeGetGregorianDate( CFAbsoluteTimeAddGregorianUnits(CFAbsoluteTimeGetCurrent(), timezone, endunits), timezone); gregorianenddate.hour = 0; gregorianenddate.minute = 0; gregorianenddate.second = 0; NSDate* startdate = [NSDate datewithtimeintervalsincereferencedate:cfgregoriandategetabsolutetime(gregorianstartdate, timezone)]; NSDate* enddate = [NSDate datewithtimeintervalsincereferencedate:cfgregoriandategetabsolutetime(gregorianenddate, timezone)]; CFRelease(timeZone); // Create the predicate. NSPredicate *predicate = [eventstore predicateforeventswithstartdate:startdate enddate:enddate calendars:nil]; // eventstore is an instance variable. // Fetch all events that match the predicate. NSArray *events = [eventstore eventsmatchingpredicate:predicate]; [self setevents:events]; 7

Fetching Events Fetching Individual Events with an Identifier You can specify a subset of calendars to search by passing an array of EKCalendar objects as the calendars parameter of the predicateforeventswithstartdate:enddate:calendars: method. You can get the user s calendars from the event store s calendars property. Passing nil tells the method to fetch from all of the user s calendars. Because the eventsmatchingpredicate: 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. Fetching Individual Events with an Identifier If you want to fetch an individual event and you know the event s unique identifier from fetching it previously with a predicate, use the EKEventStore method eventwithidentifier: to fetch the event. You can get an event s unique identifier with the eventidentifier property. Sorting Events by Start Date Applications often want to display event data to the user that is sorted by start date. To sort an array of EKEvent objects by date, call sortedarrayusingselector: on the array, providing the selector for the comparestartdatewithevent: method. 8

Using Event View Controllers The Event Kit UI framework provides two types of view controllers for manipulating events: Use the EKEventViewController class if you have an existing event you want to display or allow the user to edit. Use the EKEventEditViewController class if you allow the user to create, edit, or delete events. Displaying and Editing Events You must have an existing event you obtain from an event store to use the EKEventViewController class. You need to set the event property and any other display options before presenting this type of view controller. Listing 2-1 shows how to create an event view controller and add it to a navigation controller assuming myevent already exists. If you don t allow the user to edit the event, set the allowsediting property to NO. Listing 2-1 Editing an existing event EKEventViewController *eventviewcontroller = [[EKEventViewController alloc] init]; eventviewcontroller.event = myevent; eventviewcontroller.allowsediting = YES; navigationcontroller = [[UINavigationController alloc] [eventviewcontroller release]; initwithrootviewcontroller:eventviewcontroller]; You need to assign a delegate to an event view controller to receive a notification when the user finishes viewing the event. The delegate conforms to the EKEventViewDelegate protocol and must implement the eventviewcontroller:didcompletewithaction: method. 9

Using Event View Controllers Creating and Editing Events Creating and Editing Events To allow the user to create, edit, or delete events, use the EKEventEditViewController class and the EKEventEditViewDelegate protocol. You create an event edit view controller similar to an event view controller except that you must set the eventstore property and setting the event property is optional. If the event property is nil when you present the view controller, the user creates a new event in the default calendar and saves it to the specified event store. If you set the event property, the user edits an existing event. The event must reside in the specified event store or an exception is raised. Instances of the EKEventEditViewController class are designed to be presented modally, as shown in Listing 2-2. In this code fragment, self is the top view controller of a navigation controller. For details on modal view controllers, read Presenting a View Controller Modally in View Controller Programming Guide for ios. Listing 2-2 Presenting an event edit view controller modally EKEventEditViewController* controller = [[EKEventEditViewController alloc] init]; controller.eventstore = myeventstore; controller.editviewdelegate = self; [self presentmodalviewcontroller: controller animated:yes]; [controller release]; You must also specify a delegate to receive notification when the user finishes editing the event. The delegate conforms to the EKEventEditViewDelegate protocol and must implement the eventeditviewcontroller:didcompletewithaction: method to dismiss the modal view controller as shown in Listing 2-3. In general, the object that presents a view controller modally is responsible for dismissing it. Listing 2-3 The delegate dismisses the modal view - (void)eventeditviewcontroller:(ekeventeditviewcontroller *)controller didcompletewithaction:(ekeventeditviewaction)action { } [self dismissmodalviewcontrolleranimated:yes]; 10

Using Event View Controllers Creating and Editing Events The delegate is also passed the action that the user took when finishing the edit. The user can either cancel the changes, save the event, or delete the event. If you need to take further action, implement the eventeditviewcontroller:didcompletewithaction: delegate method. 11

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 (page 9). 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: The event s title The event s start and end dates The calendar the event is associated with You can get an array of the user s calendars with the event store property calendars. The event s recurrence rule, if it is a repeating event The alarms associated with the event 12

Creating and Editing Events Programmatically Saving Events 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. 13

Creating and Editing Events Programmatically Processing Events with a Predicate 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. 14

Creating Recurring Events Recurring events are events that repeat daily, weekly, monthly, or yearly. The pattern that repeats can be complex. For example, you might schedule an event every Tuesday and Thursday of the first and second week of every month of the year. You make an event a recurring event by assigning it a recurrence rule, which describes when the event occurs. Recurrence rules are represented by instances of the EKRecurrenceRule class. Creating a Basic Recurrence Rule You can create a recurrence rule with a simple daily, weekly, monthly, or yearly pattern with the initrecurrencewithfrequency:interval:end: method. You provide three values to this method: The recurrence frequency. This is a value of type EKRecurrenceFrequency that indicates whether the recurrence rule is daily, weekly, monthly, or yearly. The recurrence interval. This is an integer greater than 0 that specifies how often a pattern repeats. For example, if the recurrence rule is a weekly recurrence rule and its interval is 1, then the pattern repeats every week. If the recurrence rule is a monthly recurrence rule and its interval is 3, then the pattern repeats every three months. The recurrence end. This optional parameter is an instance of the EKRecurrenceEnd class, which indicates when the recurrence rule ends. The recurrence end can be based on a specific end date or on a number of occurrences. If you don t want to specify an end for the recurrence rule, pass nil. Creating a Complex Recurrence Rule You can create a recurrence rule with a complex pattern with the initrecurrencewithfrequency:interval:daysoftheweek:daysofthemonth:monthsoftheyear:weeksoftheyear:daysoftheyear:setpositions:end: method. As you do for a basic recurrence rule, you provide a frequency, an interval, and an optional end for the recurrence. In addition, you can provide the following values: Days of the week. For all recurrence rules besides daily recurrence rules, you can provide an array of EKRecurrenceDayOfWeek objects that indicate the days of the week on which the event occurs. 15

Creating Recurring Events Creating a Complex Recurrence Rule For example, you can provide an array containing EKRecurrenceDayOfWeek objects with day of week values of EKTuesday and EKFriday to create a recurrence that occurs every Tuesday and Friday. Days of the month. For monthly recurrence rules only, you can provide an array of NSNumber objects that indicate the days of the month on which the event occurs. Values can be from 1 to 31, and from 1 to 31. Negative values indicate counting backward from the end of the month. For example, you can provide an array containing the values 1 and 1 to create a recurrence that occurs on the first and last day of every month. Months of the year. For yearly recurrence rules only, you can provide an array of NSNumber objects that indicate the months of the year in which the event occurs. Values can be from 1 to 12. For example, if your originating event occurs on January 10, you can provide an array containing the values 1 and 2 to create a recurrence that occurs every January 10 and February 10. Weeks of the year. For yearly recurrence rules only, you can provide an array of NSNumber objects that indicate the weeks of the year in which the event occurs. Values can be from 1 to 53, and from 1 to 53. Negative values indicate counting backward from the end of the year. For example, if your originating event occurs on a Wednesday, you can provide an array containing the values 1 and 1 to create a recurrence that occurs on the Wednesday of the first and last weeks of every year. If a specified week does not contain a Wednesday in the current year, as can be the case for the first or last week of a year, the event does not occur. Days of the year. For yearly recurrence rules only, you can provide an array of NSNumber objects that indicate the days of the year on which the event occurs. Values can be from 1 to 366, and from 1 to 366. Negative values indicate counting backward from the end of the year. For example, you can provide an array containing the values 1 and 1 to create a recurrence that occurs on the first and last day of every year. Set positions. For all recurrence rules besides daily recurrence rules, you can provide an array of NSNumber objects that filters which occurrences to include in the recurrence rule. This filter is applied to the set of occurrences determined from the other parameters you provide. Values can be from 1 to 366, and from 1 to 366. Negative values indicate counting backward from the end of the list of occurrences. For example, you can provide an array containing the values 1 and 1 to a yearly recurrence rule that has specified Monday through Friday as its value for days of the week, and the recurrence occurs only on the first and last weekday of every year. You can provide values for any number of the above parameters (parameters that don t apply to a particular recurrence rule are ignored). If you provide a value for more than one of the above parameters, the recurrence occurs only on days that apply to all provided values. 16

Observing Event Changes It s possible for a user s Calendar database to be modified by another process or application while your application is running. If your application fetches calendar events, you should register to be notified about changes to the Calendar database. By doing so, you ensure that the calendar information you display to the user is current. Observing Notifications An EKEventStore object posts an EKEventStoreChangedNotification notification whenever it detects changes to the Calendar database. Register for this notification if your application handles event data. The following code registers for the EKEventStoreChangedNotification notification: [[NSNotificationCenter defaultcenter] addobserver:self selector:@selector(storechanged:) name:ekeventstorechangednotification object:eventstore]; Responding to Notifications When you receive an EKEventStoreChangedNotification notification, it s possible that changes have been made to EKEvent objects you fetched and retained. The effect of these changes depends on whether an event was added, modified, or deleted. If an event was added, it does not affect any of your retained events, but the added event may fall within the date range of events you are displaying to the user. If an event was modified or deleted, properties of EKEvent objects representing that event become out-of-date. Because your local data is often invalidated or incomplete when a change occurs in the Calendar database, you should release and refetch your current date range of events whenever you receive an EKEventStoreChangedNotification notification. If you are currently modifying an event and you do not want to refetch it unless it is absolutely necessary to do so, you can call the refresh method on the event. If the method returns YES, you can continue to use the event; otherwise, you need to release and refetch it. 17

Observing Event Changes Responding to Notifications Events being modified in an event view controller are updated automatically when a change occurs in the Calendar database. 18

Document Revision History This table describes the changes to Event Kit Programming Guide. Date 2010-09-22 Notes Made improvements throughout. 2010-08-03 Added a link to the SimpleEKDemo sample code. 2010-04-29 New document that explains how to access calendar data in ios with the Event Kit framework. 19

Apple Inc. 2010 Apple Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, mechanical, electronic, photocopying, recording, or otherwise, without prior written permission of Apple Inc., with the following exceptions: Any person is hereby authorized to store documentation on a single computer for personal use only and to print copies of documentation for personal use provided that the documentation contains Apple s copyright notice. The Apple logo is a trademark of Apple Inc. No licenses, express or implied, are granted with respect to any of the technology described in this document. Apple retains all intellectual property rights associated with the technology described in this document. This document is intended to assist application developers to develop applications only for Apple-labeled computers. Apple Inc. 1 Infinite Loop Cupertino, CA 95014 408-996-1010 Apple, the Apple logo, and Numbers are trademarks of Apple Inc., registered in the United States and other countries. IOS is a trademark or registered trademark of Cisco in the U.S. and other countries and is used under license. Times is a registered trademark of Heidelberger Druckmaschinen AG, available from Linotype Library GmbH. Even though Apple has reviewed this document, APPLE MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS OR IMPLIED, WITH RESPECT TO THIS DOCUMENT, ITS QUALITY, ACCURACY, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. AS A RESULT, THIS DOCUMENT IS PROVIDED AS IS, AND YOU, THE READER, ARE ASSUMING THE ENTIRE RISK AS TO ITS QUALITY AND ACCURACY. IN NO EVENT WILL APPLE BE LIABLE FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES RESULTING FROM ANY DEFECT OR INACCURACY IN THIS DOCUMENT, even if advised of the possibility of such damages. THE WARRANTY AND REMEDIES SET FORTH ABOVE ARE EXCLUSIVE AND IN LIEU OF ALL OTHERS, ORAL OR WRITTEN, EXPRESS OR IMPLIED. No Apple dealer, agent, or employee is authorized to make any modification, extension, or addition to this warranty. Some states do not allow the exclusion or limitation of implied warranties or liability for incidental or consequential damages, so the above limitation or exclusion may not apply to you. This warranty gives you specific legal rights, and you may also have other rights which vary from state to state.