iphone - 某些EKEvent与谓词不匹配

标签 iphone ios objective-c ekeventkit

我正在使用日历应用程序。

我花了整整一个月的时间来获取当天的 Activity 。我必须这样做以处理递归事件和超过一天的事件。

除以下情况外,它的工作效果非常好:几天 Activity 的最后一天。我在该 Activity 的其他几天看到了该 Activity ,但没有看到最后一个 Activity 。 (我处于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;
}

以下是 Activity 详细信息:
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)

这个问题与全日 Activity 无关。

iOS 6.X是否有相同的问题?

最佳答案

这是感染事件的代码,该代码已在我的应用中实现。

在.h中

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

在.m中
//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 ];
   }
}

希望对您有所帮助。,祝您编程愉快..谢谢

关于iphone - 某些EKEvent与谓词不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15512033/

相关文章:

ios - 在主视图中处理touchesBegan - 如何获取触摸的 subview ?

iphone - iOS - 如何检查 NSOperation 是否在 NSOperationQueue 中?

iPhone:以编程方式创建屏幕截图,然后将其添加为 subview

ios - 根据 UILabel subview 的大小更改 UITableViewCell 高度

iphone - 如何水平自动拉伸(stretch) UISegmented Control 以填充完整的 UITableViewCell?

ios - 应用程序处于后台模式时的文本转语音功能?

iPhone 的 jQuery 视差滚动?

ios - 如何获取 RTLabel 的高度

objective-c - ZXING是否与自动引用计数兼容

objective-c - iOS : Trying to add Navigation Bar to Modal UITableViewController