iPhone Event Kit : programmatically create a EKCalendar?

前端 未结 3 894
旧时难觅i
旧时难觅i 2020-12-29 14:47

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 cre

3条回答
  •  醉梦人生
    2020-12-29 15:35

    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
    }
    

提交回复
热议问题