【发布时间】:2013-12-05 18:30:52
【问题描述】:
我在尝试与现有项目集成的 iOS7 应用程序中首次运行我的应用程序时收到以下错误,但无法弄清楚如何消除此错误。
“对日历守护程序的谓词调用失败:错误域=EKCADErrorDomain Code=1013”
应用第一次运行时触发错误的代码是:
NSMutableArray *events = [NSMutableArray arrayWithArray:[self.eventStore eventsMatchingPredicate:predicate]];
在使用谓词语句之前是否需要检查 eventStore(在最后一个函数中)是否为空?
函数定义如下:
此功能确保用户已授予访问日历的权限。
// Prompt the user for access to their Calendar
-(void)requestCalendarAccess
{
if([self.eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])
{
[self.eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if (granted)
{
// The user has granted access to their Calendar; let's populate our UI with all events occuring in the next 24 hours.
[self accessGrantedForCalendar];
}
}];
}
}
授权时调用下面的函数。
// This method is called when the user has granted permission to Calendar
-(void)accessGrantedForCalendar
{
// Let's get the default calendar associated with our event store
self.defaultCalendar = PierceCalendar;
// Enable the Add button
self.addButton.enabled = YES;
// Fetch all events happening in the next 24 hours and put them into eventsList
self.eventsList = [self parseEventsByMonth];
}
函数的第一行调用包含错误导致语句的 fetchEvents 函数。
-(NSMutableArray *) parseEventsByMonth
{
NSMutableArray *allPierceEvents = [self fetchEvents];
....
return parsedEventsByMonth;
}
此函数在应用程序首次运行时执行导致错误的语句 ([self.eventStore eventsMatchingPredicate:predicate])。
// Fetch all events happening in the next 24 hours
- (NSMutableArray *)fetchEvents
{
NSMutableArray *pierceEvents = [[NSMutableArray alloc] init];
//Create the end date components
NSDateComponents *oneYearAgoComponents = [[NSDateComponents alloc] init];
oneYearAgoComponents.year = -1;
NSDate *startDate = [[NSCalendar currentCalendar] dateByAddingComponents:oneYearAgoComponents
toDate:[NSDate date]
options:0];
NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
oneYearFromNowComponents.year = 1;
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:oneYearFromNowComponents
toDate:[NSDate date]
options:0];
// We will only search the default calendar for our events
NSArray *calendarArray = [self.eventStore calendarsForEntityType:EKEntityTypeEvent];
// Create the predicate
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:calendarArray];
// Fetch all events that match the predicate
NSMutableArray *events = [NSMutableArray arrayWithArray:[self.eventStore eventsMatchingPredicate:predicate]];
for (EKEvent *event in events)
{
if (event.calendar == PierceCalendar) {
[pierceEvents addObject:event];
}
}
return pierceEvents;
}
【问题讨论】:
标签: ios7 calendar ekeventstore