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

发布于 2024-12-18 23:26:16 字数 2622 浏览 2 评论 0原文

保存时的事件标识符无法使用事件标识符获取事件。

因此,即使我们有 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:C6CDE9DAA864420BA9A2E02CD8863697000000000000000000000000000000000

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

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:040000008200E00074C5B7101A82E00800000 000646C6F747573C3010000000000000000100000000DA068F782418C4880257958003F776C

这与“eventidonsave”不同,

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

NO 刚刚移至保存后。似乎还好。

The eventIdentifier getting on save is not able to fetch the event using eventIdentifier.

So we are unable to update the EKEvent programmatically even if we have its eventIdentifier

In [SimpleEKDemo][1] provided by Apple

log the eventIdentifier on EKEventEditViewDelegate method

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

}

Run the application on device, we will get the eventIDentifier like :

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

Get the event using event identifier on next run

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

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

We will get "Not Found".

log the event ids in the place of fetching events, and run again the app

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



    for(EKEvent *eachEvent in events){

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

we can see we are getting a different eventIdentifier like 3CB60848-6CCA-43BF-B2C6-9EB9F5CFBBB7:040000008200E00074C5B7101A82E00800000000646C6F747573C3010000000000000000100000000DA068F782418C4880257958003F776C

this is not the same as 'eventidonsave'

Hi all , thanks to reading. It got solved. The above code(not its edited) is working fine.
I guess the issue was, the eventid was logged before this line
"[controller.eventStore saveEvent:.. "

NO just moved to after save. and seems to be okay.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2024-12-25 23:26:16

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

You need to save the EKEvent first and then fetch its identifier. It wil give u a null value till u dont save the Event.
[self.eventStore saveEvent:event span: EKSpanFutureEvents error:&error];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文