gpt4 book ai didi

ios - 无法在日历中添加事件

转载 作者:可可西里 更新时间:2023-11-01 05:40:15 24 4
gpt4 key购买 nike

我想在“someName”日历中添加事件。如果不存在具有给定名称的日历,那么我将以编程方式创建一个。我的问题是,当 localSource(EKSource 类型)结果为空时,事件不会被添加。我添加了 3 个检查以确保我得到了 localSource 的值,但即便如此,在某些情况下 localSource 还是 nil。因此,在我的手机上添加了事件,但在我 friend 的手机上却没有。

我关注了各种帖子,我了解到 EKSource 可以有 6 种类型:https://developer.apple.com/reference/eventkit/eksourcetype

我不明白的是在什么情况下 localSource 会是 nil?这在普通语言中是什么意思?我可以从代码中做些什么来使它不为零,还是必须由用户在设备上做些什么?

- (void)setCalendar {
NSArray *calendars = [self.eventStore calendarsForEntityType:nil];
NSString *calendarTitle = someName;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title matches %@", calendarTitle];
NSArray *filtered = [calendars filteredArrayUsingPredicate:predicate];
if ([filtered count]) {
self.calendar = [filtered firstObject];
}
else {
self.calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:self.eventStore];
self.calendar.title = calendarTitle;
EKSource *localSource;
for (EKSource *source in self.eventStore.sources)
{

//if iCloud account is setup then add the event in that calendar
if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"])
{
localSource = source;
break;
}
}
if (localSource == nil)
{
for (EKSource *source in self.eventStore.sources)
{
//if iCloud is not setup then look for local source
if (source.sourceType == EKSourceTypeLocal)
{
localSource = source;
break;
}
}

}
if (!localSource) {
localSource = [self.eventStore defaultCalendarForNewEvents].source;
}
self.calendar.source = localSource;
NSError *calendarErr = nil;
BOOL calendarSuccess = [self.eventStore saveCalendar:self.calendar commit:YES error:&calendarErr];
if (!calendarSuccess) {
NSLog(@"Error while updating calendar %@", calendarErr);
}
}

PS:我有权添加日历事件。

最佳答案

本地源描述用户日历中的“在我的 iphone 上”、“icloud”、“gmail”等字段。在我的代码中,当用户设置了 icloud 帐户但未授予 icloud 帐户写入日历的权限时,本地源为空。因此,即使应用程序具有写入日历的权限,但本地源为零,因此,向日历添加事件也会失败。

我的代码在给定的 2 种情况下添加到日历:

  1. 用户已登录 icloud 并允许 icloud 写入日历。在这种情况下,本地源不为零,并且在“iCloud”中创建名为“someName”的日历。
  2. 用户尚未登录 icloud。现在,在“我的 iphone 上”本地源中创建了名称为“someName”的日历。

关于ios - 无法在日历中添加事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41364480/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com