Search code examples
objective-cxcodenspredicatefetchekeventstore

Fetch all events in the iPhone Calendar with start date


I have application that remind patient about his/her appointment through button. if the patient click the button, I want to fetch all events in the patient calendar to check if the appointment is already exist or not. if not exist, I will create event.

I know how to add events into calendar but the fetching is return zero count??

Below is my code:

 //set start and end date
NSDate* startDate = [NSDate date];//start from today
NSDate* endDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantFuture] timeIntervalSinceReferenceDate]];//no end
NSLog(@"S Date %@", startDate);
NSLog(@"E Date %@", endDate);

NSString *AppointmentTitle=@"Appointment in Hospital at Pediatric Clinic";

EKEventStore *store = [[EKEventStore alloc] init];
NSArray *calendarArray = [store calendars];
NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];
NSArray *eventList = [store eventsMatchingPredicate:fetchCalendarEvents];
BOOL IsFound;
int EventsCount=eventList.count;
for(int i=0; i < EventsCount; i++)
{
    NSLog(@"Event Title:%@", [[eventList objectAtIndex:i] title]);
    if ([[[eventList objectAtIndex:i] title] isEqualToString:AppointmentTitle])//check title
    {
        IsFound=TRUE;
        break;
    }
    else
    {
        IsFound=FALSE;
    }
}

This code was working fine 2 months ago. Suddenly, when I come back to test it, it didn't work. Did I miss something? or Is there any mistake in my code?

I need your help plz..


Solution

  • Finaaaaaaaally I got the answer, I think Apple did something in permission ( In iOS 5, you are only allowed to access Events (EKEntityTypeEvent) in the Event Store, unlike in iOS 6, where you can access Reminders (EKEntityTypeReminder). But you need the below code to at least get granted 1 time. ) "Answered by WrightsCS"

    Find the code below:-

       EKEventStore *store = [[EKEventStore alloc] init];
    
    
    // Get the appropriate calendar
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    
    if ([store respondsToSelector:@selector(requestAccessToEntityType:completion:)])
    {
        /* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */
        [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
         {
             if ( granted )
             {
                 NSLog(@"User has granted permission!");
                 // Create the start date components
                 NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
                 oneDayAgoComponents.day = -1;
                 NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                                               toDate:[NSDate date]
                                                              options:0];
    
                 // Create the end date components
                 NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
                 oneYearFromNowComponents.year = 1;
                 NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                                    toDate:[NSDate date]
                                                                   options:0];
    
                 // Create the predicate from the event store's instance method
                 NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo
                                                                         endDate:oneYearFromNow
                                                                       calendars:nil];
    
                 // Fetch all events that match the predicate
                 NSArray *events = [store eventsMatchingPredicate:predicate];
                 NSLog(@"The content of array is%@",events);
             }
             else
             {
                 NSLog(@"User has not granted permission!");
             }
         }];
    }