2011-01-08 35 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; 
} 
相關問題