使用 EventStore,我可以创建新的 iCal 日历类型吗?
因此,在我的应用程序中,我严重依赖 iCal
,并且我可以使用 EventStore
添加事件,但仅限于“defaultCalendarForNewEvents”。我想为我在应用程序中创建的事件创建一个新日历,比如说 MyApp 日历,其行为与 iCal
日历非常相似,例如“家庭”、“工作”等
。一种以编程方式执行此操作的方法?
现在,我已经尝试过:
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSArray *calendars = [eventStore calendars];
BOOL calendarHasBeenInitialized = NO;
for(NSCalendar *cal in calendars)
{
if([cal.calendarIdentifier isEqualToString:@"Workout Tracker"])
{
calendarHasBeenInitialized = YES;
}
}
if(!calendarHasBeenInitialized)
{
NSString *string = [[NSString alloc] initWithString:@"Workout Tracker"];
NSCalendar *workoutCalendar = (__bridge NSCalendar *)(CFCalendarCreateWithIdentifier(kCFAllocatorSystemDefault, (__bridge CFStringRef)string));
EKCalendar *ekcalendar = (EKCalendar *)workoutCalendar;
NSError *error;
[eventStore saveCalendar:ekcalendar commit:YES error:&error];
}
这在我的应用程序委托中调用,如果日历不在日历数组中,我会尝试创建它。然而,这不起作用。
任何帮助将不胜感激!
So in my app I rely heavily on iCal
, and I can add events using EventStore
, but only to the "defaultCalendarForNewEvents". I want to make a new calendar just for the events I create in the app, let's say MyApp calendar, which would behave much like the iCal
calendars like "Home", "Work", etc.
Is there a way to do this programmatically?
Right now, I've tried this:
EKEventStore *eventStore = [[EKEventStore alloc] init];
NSArray *calendars = [eventStore calendars];
BOOL calendarHasBeenInitialized = NO;
for(NSCalendar *cal in calendars)
{
if([cal.calendarIdentifier isEqualToString:@"Workout Tracker"])
{
calendarHasBeenInitialized = YES;
}
}
if(!calendarHasBeenInitialized)
{
NSString *string = [[NSString alloc] initWithString:@"Workout Tracker"];
NSCalendar *workoutCalendar = (__bridge NSCalendar *)(CFCalendarCreateWithIdentifier(kCFAllocatorSystemDefault, (__bridge CFStringRef)string));
EKCalendar *ekcalendar = (EKCalendar *)workoutCalendar;
NSError *error;
[eventStore saveCalendar:ekcalendar commit:YES error:&error];
}
This is called in my App Delegate
where if the calendar is not in the calendars array, I attempt to create it. This however, does not work.
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,使用 EKCalendar 的
+calendarWithEventStore:
方法创建您指定的事件存储中的新日历。我希望它会像这样:According to the docs, use EKCalendar's
+calendarWithEventStore:
method to create a new calendar in the event store you specify. I expect it'd go like this: