简体   繁体   English

在iOS上使用eventWithIdentifier的EKEvent

[英]EKEvent with eventWithIdentifier on iOS

If I want to retrieve EKEvent from EKEventStore with eventWithIdentifier method for previously saved event but I always get null. 如果我想使用eventWithIdentifier方法从EKEventStore检索EKEvent以用于以前保存的事件,但我总是得到null。

This is the code for adding event: 这是添加事件的代码:

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);
    }
}

And code for removing event: 以及删除事件的代码:

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

Why I cannot remove previously added event from calendar with the same event id ? 为什么我无法从具有相同事件ID的日历中删除以前添加的事件?

This code was tested on iOS 5 (iPad 1) and iOS 6 (new iPad)... 此代码在iOS 5(iPad 1)和iOS 6(新iPad)上进行了测试......

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. 我使用newEvent.eventIdentifier而不是newEvent.calendarItemIdentifier ,到目前为止,使用[store eventWithIdentifier:_project.event_identifier] ,我可以检索,删除和编辑现有事件。 you should try. 你应该试试。

The documentation warns that "If the calendar of an event changes, its identifier most likely changes as well." 文档警告说“如果事件的日历发生变化,其标识符很可能也会发生变化。” Did anything change about the event between the time you stored the eventIdentifer and when you attempted to delete it? 在您存储eventIdentifer和尝试删除它之间的事件有什么变化吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM