我想在我的应用程序中插入事件,以便它们可以在iPhone Calendar.app中查看。但由于我不想将用户事件与我的应用程序混合,我想创建一个类似“MyApp Events”的EKCalendariPhone事件工具包:以编程方式创建一个EKCalendar?
这可能吗?你会如何过滤你的事件?
谢谢!
我想在我的应用程序中插入事件,以便它们可以在iPhone Calendar.app中查看。但由于我不想将用户事件与我的应用程序混合,我想创建一个类似“MyApp Events”的EKCalendariPhone事件工具包:以编程方式创建一个EKCalendar?
这可能吗?你会如何过滤你的事件?
谢谢!
这是绝对有可能创建自己的日历 - 渔获是,你需要的iOS 5:
EKEventStore* eventStore = [[EKEventStore alloc] init];
NSString* calendarName = @"My Cal";
EKCalendar* calendar;
// Get the calendar source
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}
if (!localSource)
return;
calendar = [EKCalendar calendarWithEventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;
NSError* error;
bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
if (error != nil)
{
NSLog(error.description);
// TODO: error handling here
}
你(或其他人)在添加新日历方面有什么进展吗?
我有同样的情况。我可以通过编程方式将事件完美地添加到默认日历中,但我想将它们添加到新日历中,这样它们不会干扰用户存在的事件,并且可以很容易地被用户删除/隐藏,而不是删除所有事件手动。
您无法设置新的EKCalendar对象的属性。它看起来像只能将一个像defaultCalendarForNewEvents这样的现有类型分配给EKCalendar对象。
但是,我知道有可能以编程方式创建新日历,因为我已经看到iPhone应用程序正在执行此操作(无需离开应用程序)。
这是你可以检查出是否日历已经与特定标题的存在。 如果它不存在,那么你可以编程创建它。
声明一个布尔类型变量
BOOL doesExist=NO;
EKEventStore *eventStore=[[EKEventStore alloc] init];
NSArray *calanders=[eventStore calendarsForEntityType:EKEntityTypeEvent];
//Now Iterate through every calendar in the array and match its title
// with the title that you want to create
for(EKCalendar calendar in calendars)
{
if([[calendar title] isEqualToString:@"youdesiredname"])
{
doesExist=YES;
}
}
//所以现在检查,如果我们的布尔变量包含值YES这意味着具有相同名称/标题日历已经exists.if没有,那么你可以创建
if(!doesExist)
{
NSString* calendarName = @"DesiredCalendarName";
EKCalendar* calendar;
EKSource* localSource;
for (EKSource* source in eventStore.sources) {
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
if (!localSource)
return;
calendar = [EKCalendar calendarWithEventStore:eventStore];
calendar.source = localSource;
calendar.title = calendarName;
EKEvent *event = [EKEvent eventWithEventStore:eventStore];
calendar = [eventStore calendarWithIdentifier:self.calendarIdentifier];
event.calendar = calendar;
// Set the start date to the current date/time and the event duration to one hour
NSDate *startDate = [NSDate date];
event.startDate = startDate;
event.endDate = [startDate dateByAddingTimeInterval:3600];
//And to save the event to the event database:
NSError *error = nil;
BOOL result = [eventStore saveEvent:event span:EKSpanThisEvent commit:YES error:&error];
if (result)
{
NSLog(@"Saved event to event store.")
}
else
{
NSLog(@"Error saving event: %@.", saveError);
}
NSError* error;
bool success= [eventStore saveCalendar:calendar commit:YES error:&error];
if (error != nil)
{
NSLog(error.description);
}
}
你会如何检查日历是否已经存在?我不想一次又一次地意外创建日历? – Slee 2011-11-24 13:35:21
当您创建日历时,有一个名为calendarIdentifier的属性是唯一标识符。存储该唯一标识符(以及事件存储标识符 - 确保没有更改),并在创建它之前检查事件存储以查看它是否存在。 – kurtzmarc 2011-11-26 04:11:12
但这里有一个问题:我发现如果我的应用程序创建了本地日历,用户将无法在以后删除它。这看起来像一个错误。 – matt 2012-02-03 20:21:50