微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在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.jb51.cc/iOS/328060.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐