1

我正在开发一个日历应用程序。

一个月来,我整天都在循环以获取这一天的事件。我必须这样做才能处理重复发生的事件和超过一天的事件。

除了一种情况外,它工作得很好:为期数天的活动的最后一天。我看到了这个事件的其他日子的事件,但没有看到最后一个事件。(我在 GMT+1 时区,这就是我有这个时间的原因)

SEARCH FOR THE LAST DAY OF EVENT
Start:  2013-03-25 23:00:00 +0000
End:    2013-03-26 22:59:59 +0000

EVENT
Start:  2013-03-24 21:00:06 +0000
End:    2013-03-26 21:00:06 +0000

No results!

这是返回当天事件的方法:

+ (NSArray *)ekEventsWithStartDate:(NSDate*)startDate endDate:(NSDate*)endDate
{
    NSLog(@"ekEventsWithStartDate:%@ endDate:%@",startDate,endDate);
    NSPredicate *predicate = [_eventStore predicateForEventsWithStartDate:startDate
                                                                  endDate:endDate
                                                                calendars:nil];

    NSArray *events = [_eventStore eventsMatchingPredicate:predicate];
    NSLog(@"events (%d):%@",[events count],events);
    return events;
}

以下是活动详情:

EKEvent <0xb0635e0> {EKEvent <0xb0635e0> 
{title =  24-26 Mars 10 PM; 
 location = ; 
 calendar = EKCalendar <0xb3c3c80> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;}; 
 alarms = (null);  
 URL = (null); 
 lastModified = 2013-03-19 22:11:10 +0000; 
 timeZone = Europe/Paris (GMT+01:00) offset 3600}; 
 location = ; 
 startDate = 2013-03-24 21:00:06 +0000; 
 endDate = 2013-03-26 21:00:06 +0000; 
 allDay = 0; 
 floating = 0; 
 recurrence = (null); 
 attendees = (null)}

以下是来自 ekEventsWithStartDate 方法的此事件 3 天的日志:

ekEventsWithStartDate:2013-03-23 23:00:00 +0000 endDate:2013-03-24 22:59:59 +0000
events (1):(
    "EKEvent <0x9b44c00> {EKEvent <0x9b44c00> {title =  24-26 Mars 10 PM; location = ; calendar = EKCalendar <0xb336870> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;}; alarms = (null); URL = (null); lastModified = 2013-03-19 22:11:10 +0000; timeZone = Europe/Paris (GMT+01:00) offset 3600}; location = ; startDate = 2013-03-24 21:00:06 +0000; endDate = 2013-03-26 21:00:06 +0000; allDay = 0; floating = 0; recurrence = (null); attendees = (null)}"
)

ekEventsWithStartDate:2013-03-24 23:00:00 +0000 endDate:2013-03-25 22:59:59 +0000
events (1):(
    "EKEvent <0xb28b970> {EKEvent <0xb28b970> {title =  24-26 Mars 10 PM; location = ; calendar = EKCalendar <0xb336870> {title = Calendar; type = Local; allowsModify = YES; color = #0E61B9;}; alarms = (null); URL = (null); lastModified = 2013-03-19 22:11:10 +0000; timeZone = Europe/Paris (GMT+01:00) offset 3600}; location = ; startDate = 2013-03-24 21:00:06 +0000; endDate = 2013-03-26 21:00:06 +0000; allDay = 0; floating = 0; recurrence = (null); attendees = (null)}"
)

ekEventsWithStartDate:2013-03-25 23:00:00 +0000 endDate:2013-03-26 22:59:59 +0000
events (0):(null)

为什么该方法返回空数组?

附属问题:有没有更好的方法来获取一个月中每一天的事件?我正在寻找更好的表现。

谢谢您的帮助!

编辑 20/03/2013: 感谢 Dhruvik,我发现我的代码非常适用于 iOS 5.X,但不适用于 iOS 6.X(未测试 4.X)。

我检查了 5.X 和 6.X 版本的事件和日期,我看到的唯一区别是事件日历时区属性:

iOS 5.X
timeZone = Europe/Paris (CET)

iOS 6.X
timeZone = Europe/Paris (UTC+01:00)

此问题与全天活动无关。

iOS 6.X 有同样的问题吗?

4

1 回答 1

4

这是我在我的应用程序中实现的 fecth Event 的代码。

在.h

@property (nonatomic, retain) EKEventStore *eventStore;
@property (nonatomic, retain) EKCalendar *defaultCalendar;
@property (nonatomic, retain) NSMutableArray *eventsList;

//This code will fecth the events from one day Before the current date..

- (void) fetchevents
{

   eventStore = [[EKEventStore alloc] init];

   eventsList = [[NSMutableArray alloc] init];

   EKEventStore *store = [[EKEventStore alloc] init];

   [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
       // handle access here
   }];

   // Get the appropriate calendar
   NSCalendar *calendar = [NSCalendar currentCalendar];

   // Create the start date components
   NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
   oneDayAgoComponents.day = -1; // From which date you have to fetch Events from calander, as per your need subtract the days from current date

   NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                              toDate:[NSDate date]
                                             options:0];

   NSLog(@"%@", oneDayAgo);

   // Create the end date components
   NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
   oneYearFromNowComponents.year = 0;
   NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents
                                                   toDate:[NSDate date]
                                                  options:0];
   NSLog(@"%@", oneYearFromNow);

   //Create the predicate from the event store's instance method
   NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo endDate:oneYearFromNow                                                                                      calendars:nil];


   NSArray *events_Array = [eventStore eventsMatchingPredicate: predicate];

   for (EKEvent *eventToCheck in events_Array)
   {
       [eventsList addObject:eventToCheck.title ];
   }
}

希望它有所帮助。,快乐编码..谢谢

于 2013-03-20T04:20:20.433 回答