Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Update or Edit EKEvent in iOS and store in native calendar using identifier?

i am using EKEvent in my app to fetch all store events and i want to edit any particular event by identifier and re-save in existing..so what should i do for that?

EKEventStore *store = [EKEventStore new];
    EKEvent *event = [EKEvent eventWithEventStore:store];

    event.title = @"abc";
    event.notes= @"this is updated notes";

    event.calendar = [store defaultCalendarForNewEvents];
    NSError *err = nil;

    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

this code is for i am using to store event, but where to pass identifier for update only particular event?

like image 257
Vivek Goswami Avatar asked Mar 25 '16 07:03

Vivek Goswami


1 Answers

You should save event identifier in database, so that you can later use it to update or delete the events. After creation of event using:

[store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];

You get access to event.eventIdentifier. Save it in database. When you want to edit particular event, just get that event using the ID stored:

-(void)updateNotification:(NSMutableDictionary *) info
{
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
     {
         if (!granted)
         {
             dispatch_async(dispatch_get_main_queue(), ^{

                 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot access Calendar" message:@"Please give the permission to add task in calendar from iOS > Settings > Privacy > Calendars" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
                 [alert show];

             });
             return;
         }

         if (error)
         {
             NSLog(@"%@", error);
         }

         //this is event ID you saved in DB. now you want to edit that event
         EKEvent *event  = [eventStore eventWithIdentifier:info[@"eventID"]]; 

         if(event) {

//You have your valid event. Modify as per your need.

             event.title     = [info valueForKey:@"title"];
             event.notes        = [info valueForKey:@"description"];
             event.startDate = [info objectForKey:@"date"];
             event.endDate   = [[NSDate alloc] initWithTimeInterval:3600*24 sinceDate:event.startDate];
             event.calendar = eventStore.defaultCalendarForNewEvents;

             [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];

         }

     }];
}

Hope it helps. Feel free to ask any query.

like image 58
NightFury Avatar answered Nov 06 '22 03:11

NightFury