EKEvent with eventWithIdentifier on iOS [英] EKEvent with eventWithIdentifier on iOS

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

问题描述

如果我要从检索 EKEventStore 检索 EKEvent eventWithIdentifier

这是添加事件的代码:

  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 respondingToSelector:@selector(calendarItemIdentifier)]){
[[NSUserDefaults standardUserDefaults] setObject:newEvent.calendarItemIdentifier forKey:self.showId ];
NSLog(@事件ID:%@,newEvent.calendarItemIdentifier);
}
else {
[[NSUserDefaults standardUserDefaults] setObject:newEvent.UUID forKey:self.showId];
NSLog(@事件ID:%@,newEvent.UUID);
}
}

删除事件的代码:

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

NSError * err;
BOOL success = YES;

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

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

为什么我无法使用相同的事件ID从日历中删除以前添加的活动? / p>

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

解决方案

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


If I want to retrieve EKEvent from EKEventStore with eventWithIdentifier method for previously saved event but I always get 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 ?

This code was tested on iOS 5 (iPad 1) and iOS 6 (new 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. you should try.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆