我想知道如何使用iOS中的EventKit从EventStore中获取所有事件.
这样我可以指定今天的所有事件:
- (NSArray *)fetchEventsForToday {
NSDate *startDate = [NSDate date];
// endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400];
// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];
// Fetch all events that match the predicate.
NSArray *events = [self.eventStore eventsMatchingPredicate:predicate];
return events;
}
Run Code Online (Sandbox Code Playgroud)
正确的应该使用NSPredicate,它是用以下创建的:
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray];
Run Code Online (Sandbox Code Playgroud)
我试过用
distantPast
distantFuture
Run Code Online (Sandbox Code Playgroud)
作为startDate和endDate,没有好处.所以来自其他Q的其他A并不是我想要的.
谢谢!
编辑 …
这是我的代码:
NSString * calID = [[NSUserDefaults standardUserDefaults] objectForKey:@"calendarIdentifier"];
EKCalendar *cal = [eventStore calendarWithIdentifier:calID];
// If calendar exists
if(cal)
{
// Retrieve all existing events until today
NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:[NSDate distantPast] endDate:[NSDate date] calendars:@[cal]];
self.events = [eventStore eventsMatchingPredicate:predicate];
if(self.events==nil)
NSLog(@"nil events!");
}
Run Code Online (Sandbox Code Playgroud)
calendarItentifier是我在程序中创建日历时存储的变量,因此不是我在错误的日历上添加事件的情况.
但是,代码无法检索日历上的过去事件,它只返回nil到self.events.但我DID在日历上添加事件.有什么能告诉我代码是否有问题?