Thomas Joulin
Thomas Joulin

Reputation: 6650

iPhone Event Kit : programmatically create a EKCalendar?

I would like to insert events in my app, so they can be viewed in iPhone Calendar.app. But since I don't want to mix the user events with those from my app, I wanted to create a EKCalendar like "MyApp Events"

Is this possible ? How would you filter your events otherwise ?

Thanks !

Upvotes: 8

Views: 7383

Answers (3)

Husrat Mehmood
Husrat Mehmood

Reputation: 2310

This is how you can check out whether a calendar already exists with specific title. If it does not exists then you can create it programatically.

Declare a Boolean Type Variable

BOOL doesExist=NO;
  EKEventStore *eventStore=[[EKEventStore alloc] init];

  NSArray *calanders=[eventStore   calendarsForEntityType:EKEntityTypeEvent];

  //Now Iterate through every calendar in the array and match its title  
  // with the title that you want to create



 for(EKCalendar calendar in calendars)
   {
         if([[calendar title] isEqualToString:@"youdesiredname"])
          {
                   doesExist=YES; 
          }

   }

// so now check if our bool variable contains value YES it means that a calendar with same name/title already exists.if no then you can create

 if(!doesExist)
    {
        NSString* calendarName = @"DesiredCalendarName";
        EKCalendar* calendar;


       EKSource* localSource;
       for (EKSource* source in eventStore.sources) {
       if (source.sourceType == EKSourceTypeLocal)
       {
        localSource = source;
        break;
       }




    if (!localSource)
        return;

       calendar = [EKCalendar calendarWithEventStore:eventStore];
       calendar.source = localSource;
       calendar.title = calendarName;


       EKEvent *event = [EKEvent eventWithEventStore:eventStore];
       calendar = [eventStore calendarWithIdentifier:self.calendarIdentifier];
       event.calendar = calendar;

      // Set the start date to the current date/time and the event duration to one hour
      NSDate *startDate = [NSDate date];
      event.startDate = startDate;
      event.endDate = [startDate dateByAddingTimeInterval:3600];

      //And to save the event to the event database:

       NSError *error = nil;
     BOOL result = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
    if (result) 
    {
    NSLog(@"Saved event to event store.")
    } 
   else 
    {
     NSLog(@"Error saving event: %@.", saveError);
    }

      NSError* error;
      bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
      if (error != nil)
      {
        NSLog(error.description);

      }

    }

Upvotes: 0

kurtzmarc
kurtzmarc

Reputation: 3137

It is absolutely possible to create your own calendar - the catch is that you need iOS 5:

EKEventStore* eventStore = [[EKEventStore alloc] init];
NSString* calendarName = @"My Cal";
EKCalendar* calendar;

// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
    if (source.sourceType == EKSourceTypeLocal)
    {
        localSource = source;
        break;
    }
}

if (!localSource)
    return;

calendar = [EKCalendar calendarWithEventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;

NSError* error;
bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
if (error != nil)
{
    NSLog(error.description);
    // TODO: error handling here
}

Upvotes: 12

jxd
jxd

Reputation: 21

Do you (or anyone else) have any progress with adding a new Calendar?

I've got the same situation. I can programmatically add events to the default calendar perfectly well, but I'd like to add them to a new calendar, so they don't interfere with the users exsisting events, and can be easily deleted/hidden by the user instead of removing all events manually.

You can't set the properties for a new EKCalendar object. It looks like you can only assign an exsiting one like defaultCalendarForNewEvents to an EKCalendar object.

However, I know it's possible to programmatically create a new calendar, because I've seen iPhone app doing this (without leaving the app).

  • Could it be that they use a workaround by doing some trick with an external ICS file?
  • Maybe it is possible to do this by "subscribing" to a local (on the iPhone/app filesystem) generated ICS file, instead of an URL. Does anyone have any experience with this?

Upvotes: 2

Related Questions