问题以编程方式使用eventIdentifier获取EKEvent

时间:2011-12-01 09:18:33

标签: ios eventkit

启用保存的eventIdentifier无法使用eventIdentifier获取事件。

因此,即使我们有eventIdentifier

,我们也无法以编程方式更新EKEvent

Apple提供的[SimpleEKDemo] [1]

在EKEventEditViewDelegate方法上记录eventIdentifier

- (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];
}

[controller.eventStore saveEvent:controller.event span:EKSpanThisEvent error:&error];
NSLog(@"thisEvent.id = %@", thisEvent.eventIdentifier);
[self.tableView reloadData];
break;

case EKEventEditViewActionDeleted:
// When deleting an event, remove the event from the event store,
// and reload table view.
// If deleting an event from the currenly default calendar, then update its
// eventsList.
if (self.defaultCalendar ==  thisEvent.calendar) {
[self.eventsList removeObject:thisEvent];
}
[controller.eventStore removeEvent:thisEvent span:EKSpanThisEvent error:&error];
[self.tableView reloadData];
break;

default:
break;
}
// Dismiss the modal view controller
[controller dismissModalViewControllerAnimated:YES];

}

在设备上运行应用程序,我们将得到eventIDentifier,如:

eventidonsave = 3CB60848-6CCA-43BF-B2C6-9EB9F5CFBBB7:C6CDE9DAA8644BA9A2E02CD8863697000000000000000000000000000000000000

下次运行时使用事件标识符获取事件

EKEventStore *eventStore1 = [[EKEventStore alloc] init];

EKEvent *event1  = [eventStore1 eventWithIdentifier: eventidonsave ];
if(event1){
      NSLog(@"event1 id = %@", event1.eventIdentifier);
}else{
      NSLog(@"Not Found");
}

我们将“未找到”。

在获取事件的位置记录事件ID,然后再次运行应用程序

- (NSArray *)fetchEventsForToday {
    ....
    ....



    for(EKEvent *eachEvent in events){

        NSLog(@"eachEvent.id = %@", eachEvent.eventIdentifier);
    }
}

我们可以看到我们正在获得一个不同的eventIdentifier,如3CB60848-6CCA-43BF-B2C6-9EB9F5CFBBB7:040000008200E00074C5B7101A82E00800000000646C6F747573C3010000000000000000100000000DA068F782418C4880257958003F776C

这与'eventidonsave'

不同

大家好,感谢阅读。它解决了。上面的代码(不是它的编辑)工作正常。 我想问题是,在此行之前记录了eventid “[controller.eventStore saveEvent:..”

保存后没有移动到。似乎还可以。

1 个答案:

答案 0 :(得分:1)

您需要先保存EKEvent然后获取其标识符。它将给你一个空值,直到你不保存事件。 [self.eventStore saveEvent:event span: EKSpanFutureEvents error:&error];