iOS9中怎樣在日曆App中建立一個任意時間之前開始的提醒(二)

來源:互聯網
上載者:User

iOS9中怎樣在日曆App中建立一個任意時間之前開始的提醒(二)

接上一篇,我們來看一下如何根據類型和名稱找到一個特定的日曆源,首先我們寫一個協助方法:
-(EKSource*)sourceInEventStore:(EKEventStore*)store sourceType:(EKSourceType)type sourceTitle:(NSString*)title{    for (EKSource *source in store.sources) {        if (source.sourceType == type && [source.title caseInsensitiveCompare:title] == NSOrderedSame) {            return source;        }    }    return nil;}

我們當然可以只通過日曆源的title來尋找,不過加上對其類型的檢查可謂是雙保險.我們在上述方法的開頭位置下斷點,在模擬器中運行App,不出意外應該會在該斷點中斷下來,我們在debug console中輸入:

po store.sources

可以看到模擬器中所有日曆源的輸出:

(lldb) po store.sources<__NSArrayI 0x787939e0>(EKSource <0x78792770> {UUID = 705E0A9A-1FD0-4B56-B7D9-CA4E268ECF90; type = Local; title = Default; externalID = (null)},EKSource <0x787939a0> {UUID = F2F63129-2812-48C0-80B8-AFCEFFF9AC84; type = Other; title = Other; externalID = (null)})

可以看到行事曆資料庫中第一個日曆源的真正名稱為Default,而後面一個名稱為Other.這就印證了我在第一篇中說的,在模擬器中顯示的第一個日曆源的名稱只是一個便於使用者理解的別名.

如果在真機中運行呢?你會發現第一個日曆源的名稱為iCloud.區別是前者是一個本地的源(EKSourceTypeLocal),後者是一個遠端源.這裡多說幾句,遠端日曆源也有很多種類型,比如:

    EKSourceTypeExchange    EKSourceTypeCalDAV

它們分別表示兩種不同的日曆通訊協議,用來同步用戶端和伺服器端上的日曆內容.感興趣的童鞋可以自行度娘穀哥搜尋.

現在我們可以肯定模擬器中的Default源是本地源,而iCloud源是一個CalDAV類型的遠程源.

下面我們就按照上面我們分析過的內容來分別擷取Default和iCloud源:

//擷取iCloud源EKSource *icloudSource = [self sourceInEventStore:store sourceType:EKSourceTypeCalDAV sourceTitle:@"iCloud"];//擷取本地Default源EKSource *localSource = [self sourceInEventStore:store sourceType:EKSourceTypeLocal sourceTitle:@"Default"];
三.擷取日曆源中的指定日曆

現在我們擷取到了一個日曆源,那麼怎麼擷取其中某一個日曆呢?日曆在EventKit的表示為一個EKCalendar的執行個體,我們同樣寫一個協助方法:

-(EKCalendar*)calendarWithTitle:(NSString*)title type:(EKCalendarType)type inSource:(EKSource*)source forEventType:(EKEntityType)eventType{    for (EKCalendar *calendar in [source calendarsForEntityType:eventType]) {        if ([calendar.title caseInsensitiveCompare:title] == NSOrderedSame && calendar.type == type) {            return calendar;        }    }    return nil;}

上面代碼很簡單,不用我再碎碎念了.

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.