使用 EventStore,我可以创建新的 iCal 日历类型吗?

发布于 2024-12-27 09:42:53 字数 1045 浏览 2 评论 0原文

因此,在我的应用程序中,我严重依赖 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清引 2025-01-03 09:42:53

根据文档,使用 EKCalendar 的 +calendarWithEventStore: 方法创建您指定的事件存储中的新日历。我希望它会像这样:

EKCalendar *newCalendar = [EKCalendar calendarWithEventStore:eventStore];

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:

EKCalendar *newCalendar = [EKCalendar calendarWithEventStore:eventStore];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文