Search code examples
iphoneeventkit

iPhone saveEvent does not save event


I am currently testing an app that downloads a list of special events via XML and provides them in a list that the user can then add to their calendar. When clicking the 'Add Event to Calendar' button, the function fires correctly, sets up the event with the proper dates and the saveEvent function returns noErr, yet the event never appears in the calendar. I have checked on two different iPhones and have had the same behaviour from both. I am not sure what is going wrong, since nothing seems to be going wrong.

My event code is as follows:

- (IBAction) addEvent:(id)sender
{
    EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];

    EKEvent *newEvent = [EKEvent eventWithEventStore:eventStore];
    newEvent.title = _event.name;

    CFTimeZoneRef zone = CFTimeZoneCopySystem();
    CFAbsoluteTime absTime = CFGregorianDateGetAbsoluteTime([_event getStartDate], zone);
    NSDate *start = [(NSDate *)CFDateCreate(nil, absTime) autorelease];

    absTime = CFGregorianDateGetAbsoluteTime([_event getEndDate], zone);
    NSDate *end = [(NSDate *)CFDateCreate(nil, absTime) autorelease];

    CFRelease(zone);

    newEvent.startDate = start;
    newEvent.endDate = end;
    newEvent.allDay = [_event getAllDay];

    newEvent.notes = _event.description;


    [newEvent setCalendar:[eventStore defaultCalendarForNewEvents]];

    NSError *err;

    [eventStore saveEvent:newEvent span:EKSpanThisEvent error:&err]; 

    if (err == noErr) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Event Added"
                              message:[NSString stringWithFormat:@"%@ successfully added to calendar!", _event.name]
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

After a clean + rebuild, I did get one of my events to show in the calendar, however it seemed to vanish as I scrolled up and down. The same thing happened for a different date that I added, and then the third test came up with nothing. Is there something that is deleting these dates when I scroll and/or exit and reenter the calendar?


Solution

  • Perhaps not a proper answer, but after an SDK / Xcode / OS update, this bug has vanished.

    The problem was not that the events were not getting added, but that they would disappear once the UITableView was scrolled. When events were added and appeared onscreen immediately upon opening the calendar, they would be visible, however if the date/event was not in view, it would appear to never be added. There were no errors when adding the dates, so debugging the problem was impossible.

    Continued testing on iOS 5.1 required an OS update to Lion as a pre-req for installing Xcode 4.3 and the newest SDK. Once the updates were made, the events added smoothly and remained in the list. No idea why, but thankfully the issue was resolved.