0

我使用的是Apple 示例代码中的日历代码。

我想在应用程序中制作我的日历以添加事件。但我不知道如何制作自己的日历并将其添加到 iOS 的默认日历应用程序中。

我收到这个错误。 “由于未捕获的异常'NSGenericException'而终止应用程序,原因:'无法直接初始化日历。使用 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];
}

* - - 编辑 - - **

使用: self.defaultCalendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore]; 在添加事件和委托调用时

// 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) { 永远不会是真的。

请帮忙。

4

0 回答 0