我正在尝试制作一个允许用户在一个指定的日历中进行事件的应用程序。
问题是:
我找不到一个解决方案来知道是否有一个带有我想使用的标题的日历。
如果列表为空,我编写一个创建日历的代码,但如果列表不为空,我需要知道是否有我需要的日历calendar.title
。
如果没有日历,我创建日历;如果有,我将事件添加到此日历。
下面是我正在使用的代码:
EKEvent *myEvent;
EKEventStore *store;
EKSource* localSource;
EKCalendar* newCal;
store = [[EKEventStore alloc] init];
myEvent = [EKEvent eventWithEventStore: store];
NSString* title = [arguments objectAtIndex:1];
NSString* location = [arguments objectAtIndex:2];
NSString* message = [arguments objectAtIndex:3];
NSString* startDate = [arguments objectAtIndex:4];
NSString* endDate = [arguments objectAtIndex:5];
NSString* calendarTitle = [arguments objectAtIndex:6];
//NSString* calID = nil;
//int i = 0;
EKCalendar* calendar = nil;
if(calendarTitle == nil){
calendar = store.defaultCalendarForNewEvents;
} else {
NSIndexSet* indexes = [store.calendars indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
*stop = false;
EKCalendar* cal = (EKCalendar*)obj;
if(cal.title == calendarTitle){
*stop = true;
}
return *stop;
}];
if (indexes.count == 0) {
//if list is empty i haven't calendars then i need to create it
for (EKSource* source in store.sources)
{
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}
if (!localSource) return;
newCal = [EKCalendar calendarWithEventStore:store];
calendar.source = localSource;
calendar.title = calendarTitle;
NSError* error;
bool success = [store saveCalendar:newCal commit:YES error:&error];
if (error != nil)
{
NSLog(error.description);
}
//calendar created
} else {
//!Empty List i need to search the calendar with the title = calendarTitle
//And if there isn't i need to create it
//calendar = [store.calendars objectAtIndex:[indexes firstIndex]];
}
}