2013-03-19 68 views
0

我正在处理日历应用。某些EKEvents与谓词不匹配

对于一个月我循环所有的一天,以获得这一天的事件。我必须这样做才能处理超过一天的重复性事件和事件。

除一个案例外,它工作得很好:一个为期几天的事件的最后一天。我看到这个事件的其他日子的事件,但不是最后一个事件。 (我在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) 

为什么该方法返回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一样的问题?

回答

3

这里是fecth事件

//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 ]; 
    } 
} 

希望它能帮助的代码,我在我的应用程序已经实现了..

在.H

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

在.M 。,快乐编码..谢谢

+0

感谢您的代码。我创建了一个新的空项目来测试它(它非常接近我的代码),而且我也遇到了同样的问题。但是,由于你的答案,我在几个iOS中进行了测试,发现我的问题不会出现在iOS 5.X上,它只是用于6.X(我不测试4.X)。我想知道这是否是苹果回归。 – Alex 2013-03-20 10:59:09

+0

okk ..和雅它可能会从苹果回归。所以你有从我的代码完整的解决方案? – Dhruvik 2013-03-20 11:05:33

+0

不,我有相同的代码,它不适用于iOS 6.X – Alex 2013-03-20 11:06:44