如何从我的iphone日历中检索事件?

时间:2012-09-08 16:13:20

标签: iphone objective-c ios xcode

如果有可能,我想问一下如何做到这一点。 你能给我一些示例代码或代码片段吗?感谢。

2 个答案:

答案 0 :(得分:1)

您想查看Event Kit,其中包含示例代码以及有关阅读日历事件的文档。

答案 1 :(得分:1)

获取所有事件的代码。有关详细信息,您可以使用上述事件工具包Apple文档。

NSDate *start = ...
NSDate *finish = ...

// use Dictionary for remove duplicates produced by events covered more one year segment
NSMutableDictionary *eventsDict = [NSMutableDictionary dictionaryWithCapacity:1024];

NSDate* currentStart = [NSDate dateWithTimeInterval:0 sinceDate:start];

int seconds_in_year = 60*60*24*365;

// enumerate events by one year segment because iOS do not support predicate longer than 4 year !
while ([currentStart compare:finish] == NSOrderedAscending) {

    NSDate* currentFinish = [NSDate dateWithTimeInterval:seconds_in_year sinceDate:currentStart];

    if ([currentFinish compare:finish] == NSOrderedDescending) {
        currentFinish = [NSDate dateWithTimeInterval:0 sinceDate:finish];
    }
    NSPredicate *predicate = [eventStore predicateForEventsWithStartDate:currentStart endDate:currentFinish calendars:nil];
    [eventStore enumerateEventsMatchingPredicate:predicate
                                      usingBlock:^(EKEvent *event, BOOL *stop) {

                                          if (event) {
                                              [eventsDict setObject:event forKey:event.eventIdentifier];
                                          }

                                      }];       
    currentStart = [NSDate dateWithTimeInterval:(seconds_in_year + 1) sinceDate:currentStart];

}

NSArray *events = [eventsDict allValues];