我的申请通过按钮提醒患者他/她的预约。 如果患者单击该按钮,我想获取患者日历中的所有事件以检查该约会是否已存在。如果不存在,我将创建活动。
我知道如何将事件添加到日历中,但提取是返回零计数??
以下是我的代码:
//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;
}
}
此代码在2个月前工作正常。突然间,当我回来测试时,它没有用。我错过了什么?或者我的代码中有错误吗?
我需要你的帮助PLZ ..
答案 0 :(得分:10)
Finaaaaaaaally我得到了答案, 我认为Apple在许可方面做了一些事 ( 在iOS 5中,您只能访问事件存储中的事件(EKEntityTypeEvent),这与iOS 6中的情况不同,在iOS 6中您可以访问提醒(EKEntityTypeReminder)。但是你需要以下代码至少获得1次授权。 )“WrightsCS回答”
找到以下代码: -
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!");
}
}];
}