Thursday, 22 March 2012

Calender integration in iPhone App


How to Add, Edit and Delete Event in Calender
and Synchronize with your App


import Event Kit First:

#import <EventKit/EventKit.h>

Add Event in Calender:

  //  Add New Event
    NSDateFormatter *dateFormate = [[NSDateFormatter alloc] init];
    [dateFormate setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *dateStartDate = [dateFormate dateFromString:[btnStartDate titleLabel].text];
    NSDate *dateEndDate = [dateFormate dateFromString:[btnEndDate titleLabel].text];
    NSLog(@"start date %@  end date %@",[dateFormate stringFromDate:dateStartDate],[dateFormate stringFromDate:dateEndDate]);
    [dateFormate release];
    NSError *err;
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *myEvent  = [EKEvent eventWithEventStore:eventStore];
    myEvent.title     = txtName.text;
    myEvent.startDate = dateStartDate;
    myEvent.endDate   = dateEndDate;
    [myEvent setCalendar:[eventStore defaultCalendarForNewEvents]];
    [eventStore saveEvent:myEvent span:EKSpanThisEvent error:&err]; 

Edit Event:

  //========== then Delete Previous Event from Calender ==================
        editTask = Dictionary of your save items
        NSDateFormatter *dateFormateEdit = [[NSDateFormatter alloc] init];
        [dateFormateEdit setDateFormat:@"yyyy-MM-dd HH:mm"];
        NSDate *dateStartDateEdit = [dateFormateEdit dateFromString:[editTask objectForKey:@"start_date"]];
        NSDate *dateEndDateEdit = [dateFormateEdit dateFromString:[editTask objectForKey:@"due_date"]];
        NSLog(@"start date %@  end date %@",[dateFormateEdit stringFromDate:dateStartDateEdit],[dateFormateEdit stringFromDate:dateEndDateEdit]);
        [dateFormateEdit release];
       
        EKEventStore *store = [[EKEventStore alloc] init];
        EKCalendar *defaultCalendar= [store defaultCalendarForNewEvents];
        NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
        NSPredicate *predicate = [store predicateForEventsWithStartDate:dateStartDateEdit endDate:dateEndDateEdit calendars:calendarArray];       
        NSArray *eventList = [store eventsMatchingPredicate:predicate];
        for (EKEvent *event in eventList) {   
            if ([event.title isEqualToString:[editTask objectForKey:@"name"]]) {
                [store removeEvent:event span:EKSpanThisEvent error:nil];
            }
        }
        [store release];
        //=======================================================================

and the Add New Event:

Delete Event:

   //========== Delete also Event from Calender ==================
    NSDateFormatter *dateFormateEdit = [[NSDateFormatter alloc] init];
    [dateFormateEdit setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *dateStartDateEdit = [dateFormateEdit dateFromString:[dicTask objectForKey:@"start_date"]];
    NSDate *dateEndDateEdit = [dateFormateEdit dateFromString:[dicTask objectForKey:@"due_date"]];
    NSLog(@"start date %@  end date %@",[dateFormateEdit stringFromDate:dateStartDateEdit],[dateFormateEdit stringFromDate:dateEndDateEdit]);
    [dateFormateEdit release];
   
    EKEventStore *store = [[EKEventStore alloc] init];
    EKCalendar *defaultCalendar= [store defaultCalendarForNewEvents];
    NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
    NSPredicate *predicate = [store predicateForEventsWithStartDate:dateStartDateEdit endDate:dateEndDateEdit calendars:calendarArray];       
    NSArray *eventList = [store eventsMatchingPredicate:predicate];
    for (EKEvent *event in eventList) {   
        if ([event.title isEqualToString:[dicTask objectForKey:@"name"]]) {
            [store removeEvent:event span:EKSpanThisEvent error:nil];
        }
    }
    [store release];
    //=======================================================================

 

No comments:

Post a Comment