我的应用程序如何在用户';s的iPhone

我的应用程序如何在用户';s的iPhone,iphone,list,calendar,eventkit,Iphone,List,Calendar,Eventkit,我正在编写一个iPhone应用程序,它将使用EventKit框架在用户日历中创建新事件。这一部分工作得很好(除了它处理时区的方式不稳定——但这是另一个问题)。我搞不懂的是如何获得用户日历的列表,以便他们可以选择将事件添加到哪个日历。我知道这是一个EKCalendar对象,但是文档没有显示任何获取整个集合的方法 提前感谢, Mark在文档中搜索会发现一个具有calendars属性的类 我猜你会这样做: EKEventStore * eventStore = [[EKEventStore alloc

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

提前感谢,


Mark

在文档中搜索会发现一个具有
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];    

我可以得到日历列表-问题是我没有得到一个用户可显示的列表。calendar.title属性对于所有这些属性都为空;我也没有看到任何类型的id属性

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


无论如何,上面的解决方案是好的

我用来获取日历名称和类型的可用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;
}

杰出的谢谢初始实验确认它正在返回一个日历数组。由于iOS 6.0中不推荐使用“calendars”属性,您应该更改为NSArray*calendars=[eventStore calendarsForEntityType:eEntityTypeEvent];^您从未释放事件存储或dict,因此这些都是内存泄漏。日历可能具有相同的标题,将其用作dict的键可能不起作用。日历属性在iOS 6中已被弃用。NSArray*日历行仅适用于iOS 6及以下版本,对于6>您需要使用以下命令:NSArray*日历=[eventStore CalendarsForentyType:EkenityTypeEvent];