Mann
Mann

Reputation: 5507

EKEvent is not saving reminder in calendar ios

I am trying to save reminder in calendar. Its been saved but with wrong date and time.

EDIT Basically i want to save reminder in calendar with start date selected from startDate picker and end date selected from endDate picker. and time selected from reminderTime picker. Any date and time.

Below is my code. What i am doing wrong here

EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
event.title = productTextField.text;

   [event setCalendar:[eventStore defaultCalendarForNewEvents]];

NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];//This is working

NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[reminderTime getDate]];

NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSDate *reminderDateAndTime = [NSDate dateWithTimeIntervalSince1970: [[startDate getDate] timeIntervalSince1970] + (minute * 60) + (hour * 60 * 60)];
 NSDate *endDateAndTime = [NSDate dateWithTimeIntervalSince1970: [[endDate getDate] timeIntervalSince1970] ];
 event.startDate = reminderDateAndTime;

event.endDate   = endDateAndTime;

    [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

Upvotes: 0

Views: 1188

Answers (2)

Mann
Mann

Reputation: 5507

I figured out what i was doing wrong. Below code works fine (posting code here for other users)

NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];


// Break the date up into components
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:[startDate getDate]];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                               fromDate:[reminderTime getDate]];

NSDateComponents *dateComponentsEnd = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                               fromDate:[endDate getDate]];

 [event setCalendar:[eventStore defaultCalendarForNewEvents]];

// Set up the fire time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
// Notification will fire in one minute
[dateComps setMinute:[timeComponents minute]];
[dateComps setSecond:[timeComponents second]];
NSDate *startDatee = [calendar dateFromComponents:dateComps];
NSDateComponents *dateCompsEnd = [[NSDateComponents alloc] init];
[dateCompsEnd setDay:[dateComponentsEnd day]];
[dateCompsEnd setMonth:[dateComponentsEnd month]];
[dateCompsEnd setYear:[dateComponentsEnd year]];
NSDate *endDatee = [calendar dateFromComponents:dateCompsEnd];
event.startDate=startDatee;
event.endDate=endDatee;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];

Upvotes: 0

Charan
Charan

Reputation: 4946

Basically i want to save reminder in calendar with start date selected from startDate picker and end date selected from endDate picker. and time selected from reminderTime picker. Any date and time.

You don't need 2 or 3 pickers as well, you can use only one picker for all this operation.

Seems like allocation is taking much when you use 3 pickers, one for start date, one for end date and other for time and that UI looks funky as well.

Then why are you using dateWithTimeIntervalSince1970: . It returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT. You don't need it.

Just take a BOOL for example:BOOL dateValue

if dateValue is true, get your start Date and if it is false get your end date.

Just get the first selected date like:

NSDate *startDate = [datePicker date];

Note: After selecting the start Date, set the minimum Date of your picker as your first selected start date.

Then user can't select the end Date before the start Date.

Upvotes: 4

Related Questions