ios - 如何从 iOS 中的 EKCalendar 获取所有事件

标签 ios xcode ios5 calendar ios6

我创建了一个包含一些事件的日历,现在我想从该日历中获取所有事件,但不知道它们的开始或结束日期是什么。

我试过的代码,从所有日历中获取所有事件,所以我可以稍后按日历 ID 来划分它们......:

NSPredicate *fetchCalendarEvents = [eventStore predicateForEventsWithStartDate:[NSDate distantPast] endDate:[NSDate distantFuture] calendars:eventStore.calendars];

NSArray *allEvents = [eventStore eventsMatchingPredicate:fetchCalendarEvents];

这使得 allEvents 返回 nil 而数据库清楚地显示日历中的事件。

谁能帮帮我?

最佳答案

我现在可以使用了。

这是我正在使用的代码:

NSDate* endDate =  [NSDate dateWithTimeIntervalSinceNow:[[NSDate distantFuture] timeIntervalSinceReferenceDate]];
NSArray *calendarArray = [NSArray arrayWithObject:cal];
NSPredicate *fetchCalendarEvents = [eventStore predicateForEventsWithStartDate:[NSDate date] endDate:endDate calendars:calendarArray];
NSArray *eventList = [eventStore eventsMatchingPredicate:fetchCalendarEvents];

for(int i=0; i < eventList.count; i++){

    NSLog(@"Event Title:%@", [[eventList objectAtIndex:i] title]);

}

从您调用该方法的正确日期开始,为我提供我正在为我的应用程序使用的日历中的所有事件。

我认为给 [NSDate distantPast] 是行不通的,因为它已经过去了或者什么的。将你的开始日期设置为 [NSDate date] 会行得通。

希望对遇到同样问题的人有所帮助。

关于ios - 如何从 iOS 中的 EKCalendar 获取所有事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12911487/

相关文章:

objective-c - 应用程序试图以模态方式呈现事件 Controller : UIImagePickerController

android - 如何为移动设备应用不同的 CSS,而不仅仅是基于媒体宽度/高度

ios - NavigationController.navigationBar.alpha AppDelegate 不工作

iOS:在 UITableViewCintroller 子类中创建默认静态表

c - 如何在 Xcode 上的 C 中输入用户的两个数字?

ios - IOS swift如何从 "multi-segue relationship page"回到主页面

ios - 在 UICollectionView 中复制标注

ios - 带有 MSAL 的 native iOS 应用程序需要第二次登录 Webview 才能到 Sharepoint Online

ios - 操作系统级别的自动化测试

objective-c - 在 iOS 上使用 Dropbox API 中的 Delta