2013-04-09 96 views
3

我有一個應用程序,通過按鈕提醒患者他/她的約會。 如果病人點擊了按鈕,我想要提取患者日曆中的所有事件以檢查約會是否已經存在。如果不存在,我會創建活動。以開始日期獲取iPhone日曆中的所有事件

我知道如何將事件添加到日曆中,但取回是返回零計數?

下面是我的代碼:

//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 *[email protected]"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

任何答覆pleeeeease? – 2013-04-11 11:41:57

回答

10

Finaaaaaaaally我得到了答案, 我認爲蘋果的確在許可 ( 東西在iOS 5中,您只能在事件存儲訪問活動(EKEntityTypeEvent) 。不像在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!"); 
     } 
    }]; 
} 
+0

謝謝!它幫助我很多.. – Clement 2017-06-14 14:42:53

相關問題