2013-03-19 92 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