2013年7月9日星期二

Event Kit, A Glance

As my project needs to add Event to Calendar in iOS System, I have a chance to take Event Kit a glance. It's available from iOS4.0, and get full function in iOS6.0.

First Add EventKit.framework to your project. Then add a new Event like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// First get the event store object
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    // Then create a new event
    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    // Set the start and end date
    NSDate *startDate = [NSDate date];
    NSDate *endDate  = [NSDate date];
    // Set properties of the new event object
    event.title     = @"Your Event Tittle Here";
    event.startDate = startDate;
    event.endDate   = endDate;
    event.allDay    = NO;
    event.location  = @"Your Event Location";
    // Set event's alarm
    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate date]];
    [event addAlarm:alarm];
    // Set event's calendar to the default calendar
    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
    // Create an NSError pointer & save the event
    NSError *error;
    [eventStore saveEvent:event span:EKSpanThisEvent error:&error];
    // Here get eventIdentifier and stroe it for further use
    // like remove event
    //event.eventIdentifier;
Using the eventIdentifier string to delete the event like this:
1
2
3
4
5
6
7
8
9
10
11
12
if (eventIdentifier && [eventIdentifier length]>0)
    {
        // Get the event store object
        EKEventStore *eventStore = [[EKEventStore alloc] init];
        // Get event with eventIdentifier we get when the event is create.
        EKEvent *event = [eventStore eventWithIdentifier:eventIdentifier];
        if (event)
        {
            // Remove event
            [eventStore removeEvent:event span:EKSpanThisEvent error:&error];
        }
    }
You can use EKEventEditViewController in EventKitUI.framework to add new event.
1
2
3
4
5
6
7
8
// Get the event store object
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    // Create the EditViewController
    EKEventEditViewController* controller =[[EKEventEditViewController alloc] init];
    controller.eventStore = eventStore;
    controller.editViewDelegate = self;
    [self presentModalViewController: controller animated:YES];
    controller = nil;

------
My new business with my wife, wholesale meddle-to-high end men's seamless underwear in the Netherlands: https://ecosharelife.com

没有评论:

发表评论