1

i am creating an iOS application, which get dates from the server and add them events to the calendar: I'm adding them with this code :

- (void)addReminderWithTitle:(NSString *)title date:(NSString *)date {
    NSString *dateString = date;
    NSDateFormatter *dateFormatter  = [NSDateFormatter new];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate *dateFromString = [dateFormatter dateFromString:dateString];
    [dateFromString dateByAddingTimeInterval:-60*30];    
   EKEventStore *eventStore = [[EKEventStore alloc] init];
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted) {
        EKEvent *event = [EKEvent eventWithEventStore:eventStore];
        event.title = title;
        event.startDate = dateFromString;
            //[NSDate date]; //today
        event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
        event.calendar = [eventStore defaultCalendarForNewEvents];
        NSError *err = nil;
        [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
        }}];
        }

and I need a function, that will delete all the events on a specific action from the user. any help?

  • Possible Duplicate: http://stackoverflow.com/questions/28321921/how-to-delete-all-the-events-of-my-app-calendar-when-app-is-deleted-from-device?rq=1 – Umair Aamir May 10 '17 at 12:30

1 Answers1

0

You need to get an array of all events from calendar and filter them in an array by some identifier, date or title so that you can get your own added events.

// Create the predicate from the event store's instance method
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow calendars:nil];

// Fetch all events that match the predicate
NSArray *events = [store eventsMatchingPredicate:predicate];

Then loop over the array of events and delete them like this

EKEventStore* store = [[EKEventStore alloc] init];
   EKEvent* event2 = [store eventWithIdentifier:[arrayofCalIDs objectAtIndex:i]];
if (event2 != nil) {  
  NSError* error = nil;
  [store removeEvent:event2 span:EKSpanThisEvent error:&error];
} 
Umair Aamir
  • 1,624
  • 16
  • 26
  • from where did you get "arrayofCalIDs" ? –  May 10 '17 at 05:46
  • After this line `NSArray *events = [store eventsMatchingPredicate:predicate];` you'd have array of `EKEvent` and if you'll run a loop on `events` array, you can get each event. PS: forget `arrayofCalIDs `, thats my code – Umair Aamir May 10 '17 at 09:02
  • i did this : for (int i = 0 ; i –  May 10 '17 at 09:11