Following is the code I'm using to fetch the events from Calendar, but I always get the array nil.
EKEventStore *eventStore = AppContext.HSCalendar.eventStore;
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:[NSDate distantPast] endDate:[NSDate distantFuture] calendars:[NSArray arrayWithObject:[eventStore calendarWithIdentifier:AppContext.HSCalendar.selectedCalendarID]]];
NSArray *arrEvent = [eventStore eventsMatchingPredicate:predicate];
How can I get all the events from the custom calendar?
Related
I have requirement to search events added to iOS calendar with title OR notes. I like to search events with [NSPredicate predicateWithFormat:#"title like 'Callback'"].
When googled I got predicateForEventsWithStartDate only. How can we fetch/search events with title OR notes in iOS.
You need 2 predicates to do this
Here a commented example :
// you init your store event
EKEventStore *store = [[EKEventStore alloc] init];
//you get the list of events
NSPredicate *datePredicate = [store predicateForEventsWithStartDate:[[NSDate date] dateByAddingTimeInterval: -86400.0] //yesterday
endDate:[NSDate date] //today
calendars:nil];
//this will return a list of EKEvent
NSArray<EKEvent *> *events = [store eventsMatchingPredicate:datePredicate];
//you create a second predicate to test on title or wethever you want
NSPredicate *textPredicate = [NSPredicate predicateWithFormat:#"title like 'Callback'"];
//here you will get the events with title like Callback
NSArray<EKEvent *> *results = [events filteredArrayUsingPredicate:textPredicate];
I'm trying to create a recurrent event into calendar through JSON API, but with exclusion occurences (/date) I don't know how to do it. I think i create a recurrent event, then I delete occurences.
update:
For example, the event repeats every weekend. However, I removed some of the weekends from the time line, creating an exception to a recurrence rule.
How can I get these excluded dates through the API?
my code:
1. CreateEvent from api json:
NSDictionary *dicEvent = [self.arrayEvents objectAtIndex:index];
EKEventStore *eventStore = [[EKEventStore alloc] init];
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
event.title = [dicEvent objectForKey:#"RecurrenceRule"];
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
event.startDate = [dicEvent objectForKey:#"startDate"];
event.endDate = [dicEvent objectForKey:#"endDate"];
EKSpan span = EKSpanFutureEvents;
[self.eventStore saveEvent:self.savedEvent span:span commit:YES error:&error];
2. then with exclusion occurrences, I think I delete occurences.
EventStore *eventStore = [[EKEventStore alloc] init];
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendars];
NSArray *results = [eventStore eventsMatchingPredicate:predicate];
for (int i = 0; i < results.count; i++) {
EKEvent *event = [results objectAtIndex:i]
if ([event.occurencesIdentifier isEqualToString: eventoccurencesIdentifier]) {
// delete occurencesIdentifier
break;
}
Please help me where I had gone wrong.
Thanks!
I fetch an array of events from Core Data. In turn, I want to filter the results to obtain any events currently taking place, ie that started within the last hour.
I'm a bit fuzzy on how to filter the array and even fuzzier on how to work with dates in an NSpredicate. I think I need to calculate a time that is one hour before the present, a second that is a short time from the futrue, and then compare these the starttime but I'm not sure how to go about it.
This is what I have so far:
//fetch from coredata
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest
error:&error];
NSMutableArray *mutableresults = [results mutableCopy];
NSDate* now = [NSDate date];
NSTimeInterval backSecondsInHour = -60 * 60;
NSTimeInterval forwardSecondsInHour = 60 * 60;
NSDate* hourAgo = [NSDate dateWithTimeInterval:backSecondsInHour sinceDate:now];
NSDate* hourFromNow = [NSDate dateWithTimeInterval:forwardSecondsInHour sinceDate:now];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"starttime >= %#&&starttime<=%#",hourAgo,hourFromNow];
NSArray *filteredEvents=[mutableresults filteredArrayUsingPredicate:predicate];
Am I on the right track here? Would appreciate any suggestions.
Instead retrieving all events every time from database just fetch only needed events...
Here is example if predicate applied to fetchRequest for all events since last hour:
NSDate *hourAgo = [[Date date] dateByAddingTimeInterval:-3600];
NSFetchRequest *fetchRequest = .....
request.predicate = [NSPredicate predicateWithFormat:"starttime >= %#", hourAgo];
I want to synchronise all events with Google Calendar. I used the following code to do this: EKEventStore *eventStore = [[[EKEventStore alloc] init] autorelease];
calendarsArray = [[NSArray alloc] init];
calendarsArray = [[eventStore calendars] retain];
EKCalendar *calendar = [calendarsArray objectAtIndex:1];
[events setCalendar:calendar];
NSError *err;
[eventStore saveEvent:events span:EKSpanThisEvent error:&err];
But I am not getting google calendar at object at index(1). So Can anybody help me to solve this problem.
Please note that calendars is deprecated in iOS 6 use this instead:
(NSArray *)calendarsForEntityType:(EKEntityType)entityType
Entity type then needs to be one of these:
typedef enum {
EKEntityTypeEvent,
EKEntityTypeReminder
} EKEntityType;
This should give you the expected result, more info: https://developer.apple.com/library/ios/documentation/EventKit/Reference/EKEventStoreClassRef/Reference/Reference.html#//apple_ref/occ/instm/EKEventStore/calendarsForEntityType:
I'm trying to get all the events from a determined EKCalendar, but if I call eventsMatching Predicate: with a predicate generated with predicateForEventsWithStartDate:endDate:calendars: and a past 'startDate' I get a null object. It's a bug? Or there is no way I can retrive the past events?
EDIT (NSData decalarations):
[[self eventStore] predicateForEventsWithStartDate:[NSDate distantPast] endDate:[NSDate distantFuture] calendars:syncedCalendars]
Try change your code to to:
NSDate* futureDate = [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantFuture] timeIntervalSinceReferenceDate]];
[[self eventStore] predicateForEventsWithStartDate:[NSDate date] endDate: futureDate calendars:syncedCalendars]
Also make sure that your syncedCalendars is NSArray type.