0

I was using the calendar code from Apple Sample Code.

I want to make my calendar in the app to add events. But I am not getting the how to make the own calendar and add it to the default calendar app of iOS.

I am getting this error. "Terminating app due to uncaught exception 'NSGenericException', reason: 'Can't directly init a calendar. Use calendarWithEventStore"

-(void)performCalendarActivity:(EKEventStore*)evtStore
{

On using 
// find local source
    EKSource *localSource = nil;
    for (EKSource *source in self.eventStore.sources) {
        if (source.sourceType == EKSourceTypeLocal) {
            localSource = source;
            break;
        }
    }

    // Get the default calendar from store.

//  self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents];
    self.defaultCalendar = [[EKCalendar alloc]init];
//    self.defaultCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
    self.defaultCalendar.CGColor = [UIColor blueColor].CGColor;
    self.defaultCalendar.title = @"MyNEWCalForApp";
//    self.defaultCalendar.allowsContentModifications = YES;
    self.defaultCalendar.source = localSource;
        NSError* error1;
        [self.eventStore saveCalendar:self.defaultCalendar commit:YES error: &error1];
        NSLog(@"error:%@",error1);
    NSLog(@"save cal id = %@", self.defaultCalendar.calendarIdentifier);
        [[NSUserDefaults standardUserDefaults] setObject:self.defaultCalendar.calendarIdentifier forKey:@"CalendarIdentifier"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        self._calIdentifier =  [[NSUserDefaults standardUserDefaults]valueForKey:@"CalendarIdentifier"];

        NSLog(@"existing cal id = %@", [[NSUserDefaults standardUserDefaults]valueForKey:@"CalendarIdentifier"]);
        self.defaultCalendar = [self.eventStore calendarWithIdentifier:_calIdentifier];

    self.eventsList = [[NSMutableArray alloc] initWithArray:0];

    // Fetch today's event on selected calendar and put them into the eventsList array
    [self.eventsList addObjectsFromArray:[self fetchEventsForToday]];

    [self.tableView reloadData];
}

*----EDIT----**

ON USING: self.defaultCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore]; On adding the event and on delegate call

// 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.
            if (self.defaultCalendar ==  thisEvent.calendar) {
                [self.eventsList addObject:thisEvent];
            }

if (self.defaultCalendar == thisEvent.calendar) { is never true.

Please help.

HDdeveloper
  • 4,396
  • 6
  • 40
  • 65
  • remove the `//` in front of `self.defaultCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];` and put `//` in front of `self.defaultCalendar = [[EKCalendar alloc]init];`? – Matthias Bauch Jul 02 '13 at 13:02
  • @MatthiasBauch I tried that. But it was not creating the new calendar in the iPad's default calendar list. And the events added were also show in green color instead of blue which i am using for this calendar. – HDdeveloper Jul 02 '13 at 13:07

0 Answers0