1

私はカレンダーアプリに取り組んでいます。

1 か月間、私は一日中ループして、この日のイベントを取得します。繰り返し発生するイベントや 1 日を超えるイベントを処理するには、これを行う必要があります。

数日間にわたるイベントの最終日という 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)

メソッドが null 配列を返すのはなぜですか?

補助的な質問: 月の各日のイベントを取得するより良い方法はありますか? より良い演奏を求めています。

ご協力ありがとうございました!

2013 年 3 月 20 日編集: 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

これは、アプリに実装したイベントをフェクトするコードです..

.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 に答える