我的应用程序如何获取用户iPhone上的日历列表

mpe*_*urn 11 iphone calendar list eventkit

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

提前致谢,

标记

Dav*_*ong 21

搜索文档会显示EKEventStore具有calendars属性的类.

我的猜测是你做的事情如下:

EKEventStore * eventStore = [[EKEventStore alloc] init];
NSArray * calendars = [eventStore calendars];
Run Code Online (Sandbox Code Playgroud)

编辑:从iOS 6开始,您需要指定是否要检索提醒日历或日历事件日历:

EKEventStore * eventStore = [[EKEventStore alloc] init];
EKEntityType type = // EKEntityTypeReminder or EKEntityTypeEvent
NSArray * calendars = [eventStore calendarsForEntityType:type];    
Run Code Online (Sandbox Code Playgroud)

  • 由于在iOS 6.0中不推荐使用属性'calendars',因此您应该更改为NSArray*calendars = [eventStore calendarsForEntityType:EKEntityTypeEvent]; (2认同)

mpe*_*urn 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;
}
Run Code Online (Sandbox Code Playgroud)