本文介绍了iOS 上带有 eventWithIdentifier 的 EKEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想使用 eventWithIdentifier 方法从 EKEventStore 中检索 EKEvent 以获取以前保存的事件,但我总是得到 null.

If I want to retrieve EKEvent from EKEventStore with eventWithIdentifier method for previously saved event but I always get null.

这是添加事件的代码:

EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *newEvent = [EKEvent eventWithEventStore:eventStore];
newEvent.title = @"Test";
newEvent.availability = EKEventAvailabilityFree;
newEvent.startDate = startDate;
newEvent.endDate = endDate;
[newEvent addAlarm:[EKAlarm alarmWithRelativeOffset:-15*60]];

newEvent.calendar = [eventStore defaultCalendarForNewEvents];

NSError *err;
BOOL success = [eventStore saveEvent:newEvent span:EKSpanThisEvent commit:YES error:&err];

if (success) {
    if ([newEvent respondsToSelector:@selector(calendarItemIdentifier)]) {
        [[NSUserDefaults standardUserDefaults] setObject:newEvent.calendarItemIdentifier forKey:self.showId];
        NSLog(@"Event ID: %@",newEvent.calendarItemIdentifier);
    }
    else {
        [[NSUserDefaults standardUserDefaults] setObject:newEvent.UUID forKey:self.showId];
        NSLog(@"Event ID: %@",newEvent.UUID);
    }
}

以及移除事件的代码:

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

NSError *err;
BOOL success = YES;

NSLog(@"Event ID: %@",[[NSUserDefaults standardUserDefaults] objectForKey:self.showId]);

EKEvent *existingEvent = [eventStore eventWithIdentifier:[[NSUserDefaults standardUserDefaults] objectForKey:self.showId]];
NSLog(@"Existing event: %@",existingEvent);
if (existingEvent != nil) {
    success = [eventStore removeEvent:existingEvent span:EKSpanThisEvent error:&err];
}
if (success) {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:self.showId];
}

为什么我不能从日历中删除以前添加的具有相同事件 ID 的事件?

Why I cannot remove previously added event from calendar with the same event id ?

此代码已在 iOS 5(iPad 1)和 iOS 6(新 iPad)上测试...

推荐答案

我正在使用 newEvent.eventIdentifier 而不是 newEvent.calendarItemIdentifier 到目前为止,使用 [store eventWithIdentifier:_project.event_identifier],我可以检索、删除和编辑现有事件.你应该试试.

I am using newEvent.eventIdentifier instead of newEvent.calendarItemIdentifier and so far, using [store eventWithIdentifier:_project.event_identifier], I can retrieve, delete and edit an existing event. you should try.

这篇关于iOS 上带有 eventWithIdentifier 的 EKEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-31 07:59