加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 17:56:49 所属栏目:百科 来源:网络整理
导读:我已经创建了一个包含一些事件的日历,现在我想从该日历中获取所有事件,而不知道他们的开始日期或结束日期是什么. 我尝试过的代码,从所有日历中获取所有事件,因此我可以稍后按日历ID将它们分开..: NSPredicate *fetchCalendarEvents = [eventStore predicate
我已经创建了一个包含一些事件的日历,现在我想从该日历中获取所有事件,而不知道他们的开始日期或结束日期是什么.

我尝试过的代码,从所有日历中获取所有事件,因此我可以稍后按日历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]不会起作用,因为它已经过去了或者什么……将你的startDate设置为[NSDate date]会起作用.

希望这可以帮助那些有同样问题的人.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读