11

EventKit フレームワークを使用してユーザーのカレンダーに新しいイベントを作成する iPhone アプリを作成しています。その部分はかなりうまく機能します(タイムゾーンを処理する方法が不安定であることを除けば、それは別の問題です)。私が理解できないのは、ユーザーのカレンダーのリストを取得して、イベントを追加するカレンダーを選択できるようにする方法です。それが EKCalendar オブジェクトであることは知っていますが、ドキュメントにはコレクション全体を取得する方法が示されていません。

前もって感謝します、

マーク

4

3 に答える 3

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];    
于 2011-01-08T19:17:40.843 に答える
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;
}
于 2011-04-06T11:11:11.843 に答える
2

カレンダーのリストは正常に取得できます。問題は、ユーザーが表示可能なリストを取得できないことです。それらすべての calendar.title プロパティは null です。id プロパティも表示されません。

-> 更新: 今はうまくいきます。私が犯した間違いは、eventStore オブジェクトを一時変数に入れてから、カレンダーのリストを取得してから、eventStore を解放したことです。そうすれば、すべてのカレンダーも消えてしまいます。一部の iOS フレームワークでは、コンテインメントは厳密にはオブジェクト指向ではありません。これはその例です。つまり、カレンダー オブジェクトはイベント ストアに依存しており、独自の別個のエンティティではありません。

とにかく - 上記の解決策は問題ありません!

于 2011-04-06T03:18:45.567 に答える