美文网首页
关于iOS 自定义日历功能以报错Error Domain=EK

关于iOS 自定义日历功能以报错Error Domain=EK

作者: 夏小逗gofey | 来源:发表于2017-12-12 15:49 被阅读237次

处理报错

Error Domain=EKErrorDomain Code=1 "尚未设定日历。" UserInfo={NSLocalizedDescription=尚未设定日历。}

出现这个错误的原因是由于你在为事件设置日历时,设置了nil,如下:

[event setCalendar:[self.eventStore defaultCalendarForNewEvents]];

也就是[self.eventStore defaultCalendarForNewEvents] 为nil。

出现这个问题的原因并不是代码出现了问题,暂时我也没有办法从代码上解决这个问题,因为现在用户自己都无法在日历中插入事件,解决办法如下图

自定义日历

如果你想添加属于自己App的日历的话,可以这样

- (EKCalendar *)getCalendar{

EKCalendar *calendar = nil;

BOOL needAdd = YES;

for (EKCalendar *ekcalendar in [_eventStore calendarsForEntityType:EKEntityTypeEvent]) {

if ([ekcalendar.title isEqualToString:@"My calendar"]) {

needAdd = NO;

calendar = ekcalendar;

break;

}

}

if (needAdd) {

EKSource *localSource = nil;

for (EKSource *source in _eventStore.sources)

{

//iCloud 是否存在

if (source.sourceType == EKSourceTypeCalDAV && [source.title isEqualToString:@"iCloud"])

{

localSource = source;

break;

}

}

if (localSource == nil)

{

//本地 是否存在

for (EKSource *source in _eventStore.sources) {

if (source.sourceType == EKSourceTypeLocal)

{

localSource = source;

break;

}

}

}

if (localSource) {

calendar = [EKCalendar calendarForEntityType:EKEntityTypeEvent eventStore:_eventStore];

calendar.source = localSource;

calendar.title = APPSetting.kApp_Name;//自定义日历标题

calendar.CGColor = [UIColor yellowColor].CGColor;//自定义日历颜色

NSError* error;

[_eventStore saveCalendar:calendar commit:YES error:&error];

}

}

return calendar;

}

将这个方法生成的日历设置为事件的日历

[event setCalendar:[self getCalendar]];

效果如下

iOS 日历插入事件基本功能

请参照

http://www.jianshu.com/p/2642b0e3cac5

这边就不予以赘述了

相关文章

网友评论

      本文标题:关于iOS 自定义日历功能以报错Error Domain=EK

      本文链接:https://www.haomeiwen.com/subject/dvlnixtx.html