如何从iPhone日历中检索所有事件?

如何从iPhone日历中检索所有事件?,iphone,calendar,eventkit,Iphone,Calendar,Eventkit,我想知道如何以编程方式读取iPhone日历中的所有事件 我知道检索已知日期事件的方法。以下代码检索当前日期的事件。但我如何在iPhone日历中检索所有保存的事件(即不依赖日期) NSDate *startDate = [NSDate date]; // endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:

我想知道如何以编程方式读取iPhone日历中的所有事件

我知道检索已知日期事件的方法。以下代码检索当前日期的事件。但我如何在iPhone日历中检索所有保存的事件(即不依赖日期)

NSDate *startDate = [NSDate date];

// endDate is 1 day = 60*60*24 seconds = 86400 seconds from startDate
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:(86400*1)];

// Create the predicate. Pass it the default calendar.
NSArray *calendarArray = [NSArray arrayWithObject:defaultCalendar];
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate endDate:endDate calendars:calendarArray]; 

// Fetch all events that match the predicate.
NSArray *events = [self.eventStore eventsMatchingPredicateredicate];

您可以使用三种方法从用户的日历数据库获取事件:1)将事件作为事件存储获取;2)使用谓词获取事件;3)使用标识符获取单个事件

您可以使用谓词或标识符,按照IOS指南查看一些示例和详细信息


希望这有帮助。

此bello代码用于从设备日历获取所有日历事件 只需复制粘贴下面的代码,您将获得all事件

EventKitUI/EventKitUI.h
->导入此文件

EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if(granted) {
        NSDate* startDate = /* whatever value you want */
        NSDate* endDate = /* whatever value you want */
        NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
        NSArray *eventList = [store eventsMatchingPredicate:fetchCalendarEvents];
        NSLog(@"allevent:%@",eventList);
    }
}];

harakiri,你的回答不会返回任何事件。我已经试过了,也试过了`NSDate*startDate=[NSDate datewithtimeintervalencesnow:-10*365*86400];NSDate*endDate=[NSDate DATE WITH TIMEINTERVALICENCENNOW:10*365*86400]`
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if(granted) {
        NSDate* startDate = /* whatever value you want */
        NSDate* endDate = /* whatever value you want */
        NSPredicate *fetchCalendarEvents = [store predicateForEventsWithStartDate:startDate endDate:endDate calendars:nil];
        NSArray *eventList = [store eventsMatchingPredicate:fetchCalendarEvents];
        NSLog(@"allevent:%@",eventList);
    }
}];