如何从我的应用程序添加的iCal中删除事件。

时间:2013-06-25 15:25:25

标签: iphone ios objective-c calendar icalendar

我正在开发一个应用程序,当用户添加任何事件作为他的最爱时,我将该事件添加到iCal但如果用户从他最喜欢的地方删除该特定事件,我如何从iCal中删除该事件。?

这是我的代码:

    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[self.parentDetailArray valueForKey:@"start_time_num"] intValue]];
            NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
            [dateformatter setLocale:[NSLocale currentLocale]];
            [dateformatter setDateFormat:@"dd-MM-yyyy"];
            NSString *dateString=[dateformatter stringFromDate:date];

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

             if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) 
{
 [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
             if (granted){
             //---- codes here when user allow your app to access theirs' calendar.

             EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
             event.title     = title;
             event.startDate = date;
             event.endDate   = [[NSDate alloc] initWithTimeInterval:1000 sinceDate:event.startDate];

             [event setCalendar:[eventStore defaultCalendarForNewEvents]];
             NSError *err;
             [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

// Here I'm trying to get the identifier of that event but m getting only (null)
             self.str = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];
             [self.arrayofEventId addObject:self.str];

             //[self performCalendarActivity:eventStore];
             }else
             {
             //----- codes here when user NOT allow your app to access the calendar.
             }
             }];
             }
             else {
             //---- codes here for IOS < 6.0.
             EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
             event.title     = @"Testing for calendar";

             event.startDate = [[NSDate alloc] init];
             NSLog(@"%@",event.startDate);
             [event setCalendar:[eventStore defaultCalendarForNewEvents]];
             NSError *err;
             [eventStore saveEvent:event span:EKSpanThisEvent error:&err];
             //[self performCalendarActivity:eventStore];
             }

如何获取事件标识符n从iCal中删除该特定事件。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以将所有应用生成的事件ID写入文件,然后在下次加载应用时将其加载回来。这样,您可以跟踪应用创建的事件。那么你可能想尝试这种方法:

- (BOOL)removeEvent:(EKEvent *)event span:(EKSpan)span commit:(BOOL)commit error:(NSError **)error

保存:

NSString *id = [[NSString alloc] initWithFormat:@"%@", event.eventIdentifier];

删除:

EKEvent *event = [eventStore eventWithIdentifier:id];

NSError *error = nil;
[eventStore removeEvent:event span:EKSpanThisEvent error:&error];

Apple有关于EKEventStore here的文档。