Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone Event Kit : programmatically create a EKCalendar?

Tags:

iphone

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 !

like image 660
Thomas Joulin Avatar asked Oct 13 '10 13:10

Thomas Joulin


2 Answers

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
}
like image 149
kurtzmarc Avatar answered Oct 22 '22 01:10

kurtzmarc


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?
like image 34
jxd Avatar answered Oct 22 '22 00:10

jxd