关于iOS 自定义日历功能以报错Error Domain=EKErrorDomain Code=1 "尚未设定日历。" UserInfo={NSLocalizedDescription=尚未设定日历。}

处理报错

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

这边就不予以赘述了

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容