Access to reminders

Hi, I want to access to reminders saved into Reminders.app, with the following code I have as result a list of events! What's wrong?


-(NSMutableArray*)getReminders {
    NSMutableArray *result = [[NSMutableArray alloc] init];
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *now = [NSDate date];
    NSDate *startDate = [calendar dateBySettingHour:0  minute:0  second:0  ofDate:now options:0];
    NSDate *endDate   = [calendar dateBySettingHour:23 minute:59 second:59 ofDate:now options:0];
    NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
    NSArray *events = [eventStore eventsMatchingPredicate:predicate];
    if ([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error) {
            if ( granted ) {
                for (EKReminder *currentReminder in events) {
                    NSLog(@"reminder: %@", currentReminder.description);
                    [result addObject:currentReminder];
                }
            }
            else {
                @throw [NSException exceptionWithName:@"REMINDER_PERMISSION_DENIED" reason:nil userInfo:nil];
            }
        }];
    }
    return result;
}


Thanks

One bad thing is you are returning the result before the `completion` block is executed.

requestAccessToEntityType:completion: is an asynchronous method, the passed block is executed sometime later after the method call terminated and your getReminders returns an empty result.


You need to write codes using the result INSIDE the completion handler, not after the method call.

This isn't the error!!! Because when I iterate over events array, into completion block, the code print into the console the events into calendar and not the Reminders of the reminders.app!!! Please read my question!

the problem I think that is this line:


NSArray *events = [eventStore eventsMatchingPredicate:predicate];


But, how can I retrieve reminders from eventStore?

There is another object to do this?

this is the answer!


-(NSMutableArray*)getReminders {
    NSMutableArray *result = [[NSMutableArray alloc] init];
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDate *now = [NSDate date];
    NSDate *startDate = [calendar dateBySettingHour:0  minute:0  second:0  ofDate:now options:0];
    NSDate *endDate = [calendar dateBySettingHour:23 minute:59 second:59 ofDate:now options:0];
    NSPredicate *predicate = [eventStore predicateForIncompleteRemindersWithDueDateStarting:startDate ending:endDate calendars:nil];
    [eventStore fetchRemindersMatchingPredicate:predicate completion:^(NSArray *reminders) {
        for (EKReminder *reminder in reminders) {
            FSReminder *currentReminder = [[FSReminder alloc] init];
            currentReminder.title = reminder.title;
            currentReminder.time = [NSString stringWithFormat:@"%ld:%ld", reminder.startDateComponents.hour, reminder.startDateComponents.minute];
            [result addObject:currentReminder];
        }
    }];
    return result;
}
Access to reminders
 
 
Q