8

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 !

Thomas Joulin
  • 6,590
  • 9
  • 53
  • 88

3 Answers3

12

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
}
kurtzmarc
  • 3,110
  • 1
  • 24
  • 40
  • How would you check to see if the calendar existed already? I wouldn't want to create the calendar over and over again by accident? – Slee Nov 24 '11 at 13:35
  • When you create the calendar there is a property named calendarIdentifier that is a unique identifier. Store that unique identifier (and the event store identifier too - make sure that hasn't changed) and check the event store to see if it exists before creating it. – kurtzmarc Nov 26 '11 at 04:11
  • But here's the catch: I find that if my app creates a local calendar, the user is unable to delete it later. That seems like a bug. – matt Feb 03 '12 at 20:21
  • Matt - can you put an option within the app to delete the calendar? I guess this would be a problem if the app was deleted though... hmm.. – SAHM Mar 29 '12 at 17:19
  • Sweet, thanks! Been looking for how to do this. Btw in iOS 6.0+ you need to use calendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:eventStore]; instead of [EKCalendar calendarWithEventStore:eventStore] as used above. – canhazbits Oct 15 '13 at 02:29
2

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?
jxd
  • 21
  • 2
0

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);

      }

    }
Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22