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

时间:2022-01-15 07:26:35

I'm writing an iPhone app that will use the EventKit framework to create new events in the user's Calendar. That part works pretty well (except for the wonky way it handles the timezone -- but that's another issue). What I can't figure out is how to get a list of the user's calendars so that they can choose which calendar to add the event to. I know that its an EKCalendar object but the docs don't show any way to get the whole collection.

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

Thanks in advance,

提前致谢,

Mark

标记

3 个解决方案

#1


21  

Searching through the documentation reveals an EKEventStore class that has a calendars property.

搜索文档会显示一个具有日历属性的EKEventStore类。

My guess is that you'd do something like:

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

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

EDIT: As of iOS 6, you need to specify whether you want to retrieve calendars of reminders or calendars of events:

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

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

#2


7  

The code I used to get a useable NSDictionary of calendar names and types is like this:

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

#3


2  

I get the list of calendars OK - the problem is I don't get a user-displayable list. The calendar.title property is null for all of them; I don't see any kind of id property either.

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

-> Update: It works now for me. The mistake I had made was to put the eventStore object in a temporary variable, then get the list of calendars, then release the eventStore. Well if you do that all your calendars go away too. Containment is not strictly object oriented in some of the iOS frameworks, and this is an example of that. That is, the calendar object is dependent on the event store, it's not its own, separate entity.

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

Anyway -the solution above is fine!

无论如何 - 上面的解决方案很好!

#1


21  

Searching through the documentation reveals an EKEventStore class that has a calendars property.

搜索文档会显示一个具有日历属性的EKEventStore类。

My guess is that you'd do something like:

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

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

EDIT: As of iOS 6, you need to specify whether you want to retrieve calendars of reminders or calendars of events:

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

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

#2


7  

The code I used to get a useable NSDictionary of calendar names and types is like this:

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

#3


2  

I get the list of calendars OK - the problem is I don't get a user-displayable list. The calendar.title property is null for all of them; I don't see any kind of id property either.

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

-> Update: It works now for me. The mistake I had made was to put the eventStore object in a temporary variable, then get the list of calendars, then release the eventStore. Well if you do that all your calendars go away too. Containment is not strictly object oriented in some of the iOS frameworks, and this is an example of that. That is, the calendar object is dependent on the event store, it's not its own, separate entity.

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

Anyway -the solution above is fine!

无论如何 - 上面的解决方案很好!