天天看點

二、Calendar Events 讀寫

簡介

EKEventStore Class可以對Calendar database進行增删查改操作。你可以根據自定義條件(predicate:中文翻譯為謂詞,可以了解為真真假假的函數, a predicate you provide,即你提供的條件)擷取自定義事件,也可以根據唯一辨別擷取個人事件。擷取到事件後, 你可以通過EKEvent 類擷取月曆資訊, 也可以修改月曆資訊。

連接配接Event Store

IOS5:EKEventStore* store = [[EKEventStore alloc] init];
    IOS6及以後,在初始化後需要擷取權限
        store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error){
            ...
            //異步回調
        }
           

注意:

1.EKEventStore在初始化和釋放時需要時間較長, 是以不要對每個事件單獨申請對象, 而做成單例即可。

2.store必須最後釋放, 如果在其他Event Kit對象釋放前釋放, 則可能産生錯誤。

擷取Events

目前有兩種擷取事件的方法。第一種是通過謂詞或搜尋條件來擷取,隻要符合條件的事件都會擷取到。第二種是通過唯一辨別擷取,則隻會擷取到一個。 注意:從Calendar database擷取到的事件不一定按時間進行排序,如果要排序,則需要調用排序函數

使用謂詞

擷取事件時,通常會指定一段時間範圍,使用EKEventStore中的方法eventsMatchingPredicate:可以擷取指定時間範圍内的事件。

下面展示一段擷取事件的代碼:

// Get the appropriate calendar
NSCalendar *calendar = [NSCalendar currentCalendar];

// Create the start date components
NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];
oneDayAgoComponents.day = -;
NSDate *oneDayAgo = [calendar dateByAddingComponents:oneDayAgoComponents
                                              toDate:[NSDate date]
                                             options:];

// Create the end date components
NSDateComponents *oneYearFromNowComponents = [[NSDateComponents alloc] init];
oneYearFromNowComponents.year = ;
NSDate *oneYearFromNow = [calendar dateByAddingComponents:oneYearFromNowComponents 
                toDate:[NSDate date]  options:];

// Create the predicate from the event store's instance method
//NSPredicate必須通過該方法擷取
//參數calendars是一個calendar的集合,如果為nil,表示所有使用者的calendars 
NSPredicate *predicate = [store predicateForEventsWithStartDate:oneDayAgo 
                        endDate:oneYearFromNow calendars:nil];

// Fetch all events that match the predicate、
//該方法為同步方法,最好放在工作線程裡做 
NSArray *events = [store eventsMatchingPredicate:predicate];

//sort
NSMutableArray* sortedArray = [NSMutableArray arraywithArray:
                                        [events sortedArrayUsingSelector:@selector(compareStartDateWithEvent:)]];    
           

使用唯一辨別

EKEventStore eventWithIdentifier: 方法可以根據唯一辨別擷取事件,如果該事件為循環提醒事件, 則傳回第一次發生的事件。

建立和編輯事件

Note:如果開發平台是ios,你可以用Event Kit UI framework提供的event view controllers模闆進行事件的操作。

建立事件的接口,EKEvent eventWithEventStore:

事件可編輯項:

- 标題

- 開始和結束事件

- 事件關聯的月曆屬性,如月曆類型:工作、月曆、家庭;被邀請人

- 事件關聯的提醒屬性,如提醒的時間點、顯示為

- 事件規則,是否重複執行

儲存和删除事件

注意:在更改Calendar database時, 一定要讓使用者進行确認。

事件的修改将在儲存之後才生效,儲存接口: EKEventStore saveEvent:span:commit:error:

删除接口: EKEventStore removeEvent:span:commit:error:

以上兩個接口中的span參數, 如果指定為EKSpanFutureEvents, 則對于循環發生的事件, 将應用到未來出現的所有事件中

以上兩個接口中的commit參數, 如果傳NO,則必須要保證在後面的代碼中調用commit進而使操作永久生效。ps:YES時會立即生效。

當删除或儲存操作時,都會自動同步到該事件所屬的月曆(CalDAV, Exchange 等)

批處理事件操作

該方法根據predicate枚舉每個事件, 可以在block中對每個事件進行操作。

[eventStore enumerateEventsMatchingPredicate:predicate usingBlock:^(EKEvent *event, BOOL *stop){
        NSLog(@"%@", event);
        //該block方法為同步方法, 應該放到工作線程中執行
        dispatch_async(dispatch_get_global_queue(, ), ^{ 
        //....do something
    });
        //當stop=YES時,将跳出block
    }];
           

繼續閱讀