Powered By Blogger

Wednesday, March 26, 2014

how to store event in native calendar of iPhone with EventKit?

Hi,
Here, I'm going to share some lines of code by which you can add an event to native calendar of an iPhone/iPad/iPod touch.  So, please prefer below code to make this functionality visible to your project.

========================================================================

Step 1: Import these framework into your project.
#import <EventKit/EventKit.h>
#import <EventKitUI/EventKitUI.h>

Step 2: Now add below code to your working viewcontroller class. But before doing this make sure that you declare some necessary variables.

-(IBAction)addEventToCalendar:(id)sender
{
    if (!self.defaultCalendar)
    {
        self.eventStore = [[EKEventStore alloc]init];
        self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
    }
    // save to iphone calendar
    if([self.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) // >= iOS 6.0
    {
        // iOS 6 and later
        // This line asks user's permission to access his calendar
        [self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
         {
             if (granted) // user user is ok with it
             {
                 [self openCalendarEditViewController];
             }
         }
         else // if he does not allow
         {
             [self performSelectorOnMainThread:@selector(callAccessAlert) withObject:nil waitUntilDone:YES];
             return;
         }
         
         }];
    }
    else // < iOS 6.0
    {
        [self openCalendarEditViewController];
    }
}

-(void)openCalendarEditViewController
{
    NSString *address = @“”; // Address of place where the event will be hold
    NSString *businessUrl = @“”; //Url which you want to add with calendar event
    
    NSDate *startdate; //start date and time of event
    NSDate *enddate = //end date and time of event
    NSString *strNotes; //Add notes which you want to share in with Calendar event
    
    EKEventEditViewController  *addController = [[EKEventEditViewController alloc] init];
    // set the addController's event store to the current event store.
    EKEvent *event = [EKEvent eventWithEventStore:self.eventStore];
    event.title = @“”;//title of an event should be unique to display appointments separately
    event.notes = strNotes;
    event.location = address;
    event.URL = [NSURL URLWithString:businessUrl];
    
    event.startDate = startdate;
    event.endDate = enddate;//[startdate dateByAddingTimeInterval:duration];
    
    NSDate *alarmDate = [startdate dateByAddingTimeInterval:-3600];
    NSArray *arrAlarm = [NSArray arrayWithObject:[EKAlarm alarmWithAbsoluteDate:alarmDate]];
    event.alarms= arrAlarm;
    
    [event setCalendar:self.defaultCalendar];
    
    addController.event = event;
    addController.eventStore = self.eventStore;
    addController.modalPresentationStyle = UIModalPresentationPageSheet;
    addController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    addController.editViewDelegate = self;
    addController.navigationBar.tintColor=[UIColor colorWithRed:0.976 green:0.612 blue:0.067 alpha:1];
    // present EventsAddViewController as a modal view controller
    [self presentModalViewController:addController animated:YES];
}

- (void)requestAccessToEntityType:(EKEntityType)entityType completion:(EKEventStoreRequestAccessCompletionHandler)completion
{
}

// Overriding EKEventEditViewDelegate method to update event store according to user actions.
- (void)eventEditViewController:(EKEventEditViewController *)controller
          didCompleteWithAction:(EKEventEditViewAction)action {
    NSError *error = nil;
    EKEvent *thisEvent = controller.event;
    switch (action) {
        case EKEventEditViewActionCanceled:
            // Edit action canceled, do nothing.
            break;
        case EKEventEditViewActionSaved:
            // When user hit "Done" button, save the newly created event to the event store,
            // and reload table view.
            // If the new event is being added to the default calendar, then update its
            // eventsList.
        {
            NSError *err = nil;
            BOOL isSuceess=[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];
        }
            break;
        case EKEventEditViewActionDeleted:
            // When deleting an event, remove the event from the event store,
            // and reload table view.
            // If deleting an event from the currenly default calendar, then update its
            // eventsList.
            [controller.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:&error];
            break;
        default:
            break;
    }
    // Dismiss the modal view controller
    [controller dismissModalViewControllerAnimated:YES];
}

// Set the calendar edited by EKEventEditViewController to our chosen calendar - the default calendar.
- (EKCalendar *)eventEditViewControllerDefaultCalendarForNewEvents:(EKEventEditViewController *)controller {
    EKCalendar *calendarForEdit = self.defaultCalendar;
    return calendarForEdit;

}

------------------

Regards,
Nilesh M. Prajapati


1 comment:

  1. Hey Nilesh,

    really great your post. where can found the sample code???

    thank you

    ReplyDelete