即使EKEvent.hasNotes返回YES,EKEvent.notes也会返回(null)

时间:2014-09-26 05:11:14

标签: ios

我正在使用Xcode 6.0.1,使用Event Kit制作测试应用程序。以下代码成功填充了每个事件的标题,但即使hasNotes属性返回YES,其注释也会返回为(null)。而且,我可以在iPhone的默认日历应用程序上看到同一事件的注释。

我做错了什么?

- (void)viewDidLoad
{
    [super viewDidLoad];

    [eventStore requestAccessToEntityType:EKEntityTypeEvent
                               completion:^(BOOL granted, NSError *error) {
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                       if (error)
                                       {
                                           NSLog(@" !! error");
                                           // display error message here
                                       }
                                       else if (!granted)
                                       {
                                           NSLog(@"Not Granted");
                                           // display access denied error message here
                                       }
                                       else
                                       {
                                           // access granted

                                           NSCalendar *calendar = [NSCalendar currentCalendar];
                                           // Create the start date components
                                           NSDateComponents *oneWeekAgoComponents = [[NSDateComponents alloc] init];
                                           oneWeekAgoComponents.day = -1;
                                           NSDate *oneWeekAgo = [calendar dateByAddingComponents:oneWeekAgoComponents toDate:[NSDate date] options:0];
                                           // Create the end date components
                                           NSDateComponents *oneMonthFromNowComponents = [[NSDateComponents alloc] init];
                                           oneMonthFromNowComponents.month = 1;
                                           NSDate *oneMonthFromNow = [calendar dateByAddingComponents:oneMonthFromNowComponents toDate:[NSDate date] options:0];

                                           // Create the predicate from the event store's instance method
                                           NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:oneWeekAgo endDate:oneMonthFromNow calendars:nil];
                                           // Fetch all events that match the predicate
                                           _eventArray = [eventStore eventsMatchingPredicate:predicate];

                                           [self.tableView reloadData];

                                       }
                                   });
                               }];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    EKEvent *event = [self.eventArray objectAtIndex:indexPath.row];
    cell.textLabel.text = event.title;

    if (event.hasNotes) {
        cell.detailTextLabel.text = event.notes;
    } else {
        cell.detailTextLabel.text = @"";
    }

    return cell;
}

2 个答案:

答案 0 :(得分:2)

我还没有完全解决它,但得到了线索。

NSArray *events = [eventStore eventsMatchingPredicate:predicate];

这没有得到笔记。所以,我通过执行

来枚举返回的事件
 [eventStore enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop) {
                                               NSLog(@"title: %@",event.title);
                                               NSLog(@"hasNotes: %s",event.hasNotes ? "YES":"NO");
                                               NSLog(@"notes: %@",event.notes);
                                               NSLog(@"-----");
                                               [_eventTitles addObject:event.title];
                                               [_eventTitles addObject:event.hasNotes ? event.notes : @""];
                                           }];

这个返回实际音符(null)。

答案 1 :(得分:0)

我有一个类似的问题,但从EKEvent访问日历对象时。我的情况是因为我在尝试访问日历之前(EKEventStore)发布了cellForRowAtIndexPath:实例。

根据Apple文档“阅读和编写日历事件”:

  

事件存储实例不得在其他Event Kit之前发布   对象;否则,可能会发生未定义的行为。

https://developer.apple.com/library/mac/documentation/DataManagement/Conceptual/EventKitProgGuide/ReadingAndWritingEvents.html#//apple_ref/doc/uid/TP40004775-SW1