hacker
hacker

Reputation: 8957

How to add Events on specific days in iOS?

I have an application in which I need to add the events for the particular dates, coming from server. I'm doing it like below:

[dateFormatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSDate *strtdate=[dateFormatter dateFromString:[replacedDict valueForKey:@"Departure"]];
NSPredicate *predicateForEventOnDate = [store predicateForEventsWithStartDate:strtdate endDate:[strtdate dateByAddingTimeInterval:60*60*24] calendars:calendarArray];

[store enumerateEventsMatchingPredicate:predicateForEventOnDate usingBlock:^(EKEvent *event1, BOOL *stop) {
NSLog(@"title: %@",event1.title);
NSLog(@"hasNotes: %s",event1.hasNotes ? "YES":"NO");
NSLog(@"notes: %@",event1.notes);
NSLog(@"-----");
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:event1.title, @"title",event1.hasNotes ? event1.notes : @"",@"notes", nil];

[eventOnDate addObject:dict];
}];

NSLog(@"%@",eventOnDate);
if (eventOnDate.count > 0)
{
    for (int i=0;i<[eventOnDate count];i++) {
        if (![[[eventOnDate objectAtIndex:i] valueForKey:@"notes"] isEqualToString:note])
        {

            EKEvent *event = [EKEvent eventWithEventStore:store];
            event.title = subject; //give event title you want
            event.notes = note ;
            event.startDate = strtdate;
            event.endDate = [event.startDate dateByAddingTimeInterval:60*60*24];
            event.calendar = [store defaultCalendarForNewEvents];
            NSError *err = nil;
            [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
            if (!err) {
            }
            else
            {
                //  NSLog(@" Event not created");

            }
        }
        else if ([[[eventOnDate objectAtIndex:i]valueForKey:@"notes"] isEqualToString:note])
        {

        }
    }
}

But here the event gets added to just the todays date only also i am getting warning messages like Cal Database Change Notification has changed notification 180 times like that, . can anybody tell me where i am going wrong

I need to add the events to particular day? Can anybody help me on this?

Upvotes: 1

Views: 1040

Answers (1)

Mohamed Jaleel Nazir
Mohamed Jaleel Nazir

Reputation: 5831

Check your date format is Correct.

event.startDate should be NSDate

EKEventStore *store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
      if (!granted) return;
      EKEvent *event = [EKEvent eventWithEventStore:store];
      event.title = @"Event Title";
      event.startDate = [NSDate date]; // today
      event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  // Duration 1 hr
      [event setCalendar:[store defaultCalendarForNewEvents]];
      NSError *err = nil;
      [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
      NSString *savedEventId = event.eventIdentifier;  // Store this so you can access this event later
    }];

Read this blogs...

http://samwize.com/2014/07/09/how-to-add-edit-and-remove-ios-calendar-events/

https://developer.apple.com/library/ios/documentation/EventKit/Reference/EKEventStoreClassRef/

Upvotes: 1

Related Questions