2011-01-08 36 views
11

我正在编写一个iPhone应用程序,该应用程序将使用EventKit框架在用户的日历中创建新事件。这部分工作得很好(除了处理时区的怪异方式 - 但这是另一个问题)。我无法弄清楚的是如何获取用户日历的列表,以便他们可以选择要将事件添加到哪个日历。我知道它是一个EKCalendar对象,但文档没有显示任何获取整个集合的方法。我的应用程序如何获取用户iPhone上的日历列表

由于提前,

马克

回答

21

通过文档搜索揭示一个EKEventStore类具有calendars属性。

我的猜测是,你会做这样的事情:

EKEventStore * eventStore = [[EKEventStore alloc] init]; 
NSArray * calendars = [eventStore calendars]; 

编辑:作为iOS 6后,您需要指定是否要检索事件的提醒日历或日历:

EKEventStore * eventStore = [[EKEventStore alloc] init]; 
EKEntityType type = // EKEntityTypeReminder or EKEntityTypeEvent 
NSArray * calendars = [eventStore calendarsForEntityType:type];  
+0

好极了!谢谢!初始实验确认它正在返回日历数组。 – mpemburn 2011-01-08 21:15:32

+2

作为属性 '日历' 在IOS 6.0已过时,你应该改变到 的NSArray *日历= [eventStore calendarsForEntityType:EKEntityTypeEvent]; – TwiterZX 2013-09-06 09:55:04

2

我得到的日历列表好 - 问题是我没有得到用户可显示的列表。 calendar.title属性对于所有这些属性均为null;我也没有看到任何类型的ID属性。

- >更新:现在适用于我。我犯的错误是将eventStore对象放入临时变量中,然后获取日历列表,然后释放eventStore。那么如果你这样做,所有的日历也会消失。在一些iOS框架中,遏制不是严格的面向对象的,这就是一个例子。也就是说,日历对象依赖于事件存储,它不是它自己的独立实体。

无论如何 - 上述解决方案是好的!

+0

得到这个工作很好。在我的答案中查看完整信息。 – mpemburn 2011-04-06 11:05:46

7

我用来获取日历名称和类型的可用的NSDictionary的代码是这样的:

//*** Returns a dictionary containing device's calendars by type (only writable calendars) 
- (NSDictionary *)listCalendars { 

    EKEventStore *eventDB = [[EKEventStore alloc] init]; 
    NSArray * calendars = [eventDB calendars]; 
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    NSString * typeString = @""; 

    for (EKCalendar *thisCalendar in calendars) { 
     EKCalendarType type = thisCalendar.type; 
     if (type == EKCalendarTypeLocal) { 
      typeString = @"local"; 
     } 
     if (type == EKCalendarTypeCalDAV) { 
      typeString = @"calDAV"; 
     } 
     if (type == EKCalendarTypeExchange) { 
      typeString = @"exchange"; 
     } 
     if (type == EKCalendarTypeSubscription) { 
      typeString = @"subscription"; 
     } 
     if (type == EKCalendarTypeBirthday) { 
      typeString = @"birthday"; 
     } 
     if (thisCalendar.allowsContentModifications) { 
      NSLog(@"The title is:%@", thisCalendar.title); 
      [dict setObject: typeString forKey: thisCalendar.title]; 
     } 
    } 
    return dict; 
} 
相关问题