xcode 如何从 iOS 中的 EKCalendar 获取所有事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12911487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get all events from an EKCalendar in iOS
提问by Lofens
I've created a calendar with some events, now I want to get all events from that calendar, without knowing what their start or end date is.
我已经创建了一个包含一些事件的日历,现在我想从该日历中获取所有事件,而不知道它们的开始或结束日期是什么。
Code I tried, getting all events from all calendars, so I could divide them by calendar ID later or so.. :
我试过的代码,从所有日历中获取所有事件,所以我可以稍后按日历 ID 划分它们..:
NSPredicate *fetchCalendarEvents = [eventStore predicateForEventsWithStartDate:[NSDate distantPast] endDate:[NSDate distantFuture] calendars:eventStore.calendars];
NSArray *allEvents = [eventStore eventsMatchingPredicate:fetchCalendarEvents];
This makes allEvents
return nil
while the database clearly shows events in the calendar.
这在数据库清楚地显示日历中的事件时allEvents
返回nil
。
Anyone could help me?
任何人都可以帮助我吗?
回答by Lofens
I have it working now.
我现在可以工作了。
This is the code I'm using:
这是我正在使用的代码:
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]);
}
Gives me all events from the calendar I'm using for my app from the date it is right when you call the method.
从您调用该方法的正确日期开始,我为我的应用程序提供日历中的所有事件。
I think giving [NSDate distantPast]
won't work because it's in the past or something.. setting your startDate as [NSDate date]
will work.
我认为给予是[NSDate distantPast]
行不通的,因为它已经过去了,或者什么的……设置你的 startDate[NSDate date]
会起作用。
Hope this helps people who have the same problem..
希望这可以帮助有同样问题的人..
回答by Momchil Marinov
If you would like to lookup a year span greater than 4 years, the returned events would be trimmed by the system to those from the first 4 years. (see Apple's documentation). What could be a possible solution is to run the predicate matching in batches.
如果您想查找超过 4 年的年份,系统会将返回的事件修剪为前 4 年的事件。(请参阅Apple 的文档)。可能的解决方案是批量运行谓词匹配。
Here's a C# extension for Xamarin.iOS
这是 Xamarin.iOS 的 C# 扩展
public static class EKEventStoreExtensions
{
private const int MaxPredicateYearSpan = 4;
public static EKEvent[] GetAllEvents(this EKEventStore eventStore, DateTimeOffset startAt, DateTimeOffset endAt, params EKCalendar[] calendars)
{
var isBatched = endAt.Year - startAt.Year >= MaxPredicateYearSpan;
var result = new List<EKEvent>();
var batchStartAt = startAt;
var batchEndAt = endAt;
while (batchStartAt < endAt)
{
if (isBatched)
{
batchEndAt = batchStartAt.AddYears(1);
if (batchEndAt > endAt)
{
batchEndAt = endAt;
}
}
var events = GetEventsMatching(eventStore, batchStartAt, batchEndAt, calendars);
result.AddRange(events);
batchStartAt = batchEndAt;
}
return result.ToArray();
}
private static EKEvent[] GetEventsMatching(EKEventStore eventStore, DateTimeOffset startAt, DateTimeOffset endAt, EKCalendar[] calendars)
{
var startDate = (NSDate)startAt.LocalDateTime;
var endDate = (NSDate)endAt.LocalDateTime;
var fetchCalendarEvents = eventStore.PredicateForEvents(startDate, endDate, calendars);
return eventStore.EventsMatching(fetchCalendarEvents);
}
}