在iOS上使用eventWithIdentifier的EKEvent

前端之家收集整理的这篇文章主要介绍了在iOS上使用eventWithIdentifier的EKEvent前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我想使用eventWithIdentifier方法从EKEventStore检索EKEvent以用于以前保存的事件,但我总是得到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的日历中删除以前添加的事件?

代码在iOS 5(iPad 1)和iOS 6(新iPad)上进行了测试……

解决方法

我使用newEvent.eventIdentifier而不是newEvent.calendarItemIdentifier,到目前为止,使用[store eventWithIdentifier:_project.event_identifier],我可以检索,删除和编辑现有事件.你应该试试.
原文链接:https://www.f2er.com/iOS/328060.html

猜你在找的iOS相关文章