I am trying to save/ retrieve reminders from my app, but for some reason the EKReminder seems as though it is not being saved. Here is my code:
EKEventStore * _eventStore = [[EKEventStore alloc] init];
[_eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
//create a new calendar for reminders.
EKCalendar *calendar = [EKCalendar calendarForEntityType:EKEntityTypeReminder eventStore:_eventStore];
EKSource *localSource = nil;
for (EKSource *source in _eventStore.sources)
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
calendar.source = localSource;
self.calendarIdentifier = calendar.calendarIdentifier;
EKReminder *reminder = [EKReminder reminderWithEventStore:_eventStore];
reminder.calendar = calendar;
reminder.title = @"Test Reminder";
reminder.startDateComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit fromDate:[NSDate date]];
reminder.completionDate = [NSDate dateWithTimeIntervalSinceNow:100];
EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:[NSDate dateWithTimeIntervalSinceNow:100]];
reminder.alarms = @[alarm];
NSError *errorOb = nil;
BOOL saved = [_eventStore saveReminder:reminder commit:YES error:&errorOb];
if (saved) NSLog(@"Saved Reminder");
else NSLog(@"Failed to save reminder");
}];
Then later on, I am trying to verify that the reminder has in fact been saved. I do that by using this code:
NSPredicate *predicate = [_eventStore predicateForRemindersInCalendars:nil];
[_eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
for (EKReminder *reminder in reminders) {
NSLog(@"Reminder Title: %@", reminder.title);
}
}];
And my new reminder never shows up!
Can anyone tell me what I am doing wrong here??
I got this to work by switching up the localSource to be EKSourceTypeCalDAV. Not totally sure why that was required, maybe someone can shed some light on that?
Hope this works for you.
And check my this post if you are having trouble in saving an event to device calendar.
You only need to call
-requestAccessToEntityType:completion:
if the authorization status isEKAuthorizationStatusNotDetermined
. You can check the authorization status using+[EKEventStore authorizationStatusForEntityType:]
.The completion block will then only be called the first time you call
-requestAccessToEntityType:completion
. On the Simulator it won't even be called once, because on the Simulator your app will already have access.