In iOS9, how to create a reminder that starts before any time in the calendar App (2)
Next, let's take a look at how to find a specific calendar source based on the type and name. First, let's write a help method:
-(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;}
Of course, we can only search through the title of the calendar source, but adding the check for its type is double insurance. when we break a breakpoint at the beginning of the above method and run the App in the simulator, the breakpoint should be interrupted, and we enter in the debug console:
po store.sources
You can see the output of all calendar sources in the simulator:
(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)})
The real name of the first calendar source in the calendar database is Default, and the name of the last calendar source is Other. this confirms what I said in the first article. The name of the first calendar source displayed in the simulator is just an alias that is easy for users to understand.
What if it runs on a real machine? You will find that the first calendar source is named iCloud. the difference is that the former is a local source (EKSourceTypeLocal), and the latter is a remote source. there are also many types of remote calendar sources, such:
EKSourceTypeExchange EKSourceTypeCalDAV
They represent two different calendar communication protocols, which are used to synchronize the calendar content on the client and server respectively. Interested shoes can be searched by Niang Gu Ge.
Now we can be sure that the Default source in the simulator is the local source, while the iCloud source is a CalDAV remote source.
Next we will obtain the Default and iCloud sources respectively based on the content we analyzed above:
// Obtain the EKSource * icloudSource = [self sourceInEventStore: store sourceType: EKSourceTypeCalDAV sourceTitle: @ "iCloud"]; // obtain the local Default source EKSource * localSource = [self sourceInEventStore: store sourceType: EKSourceTypeLocal sourceTitle: @ "Default"];
3. Obtain the specified calendar in the calendar Source
Now we get a calendar. How can we get a calendar? Calendar is represented as an EKCalendar instance in EventKit. We also write a help method:
-(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;}
The above code is very simple, and I don't need to break it down.