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

本文介绍了解决在使用iOS日历SDK时遇到的“尚未设定日历”错误的方法。通过检查并设置正确的日历对象,确保应用能够成功地在用户的设备上创建日历事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

处理报错

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 *ekcalendarin [_eventStorecalendarsForEntityType:EKEntityTypeEvent]) {

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

            needAdd = NO;

            calendar = ekcalendar;

            break;

        }

    }

    if (needAdd) {

        EKSource *localSource = nil;

        for (EKSource *sourcein_eventStore.sources)

        {

            //iCloud 是否存在

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

            {

                localSource = source;

                break;

            }

        }

        if (localSource == nil)

        {

            //本地 是否存在

            for (EKSource *sourcein_eventStore.sources) {

                if (source.sourceType ==EKSourceTypeLocal)

                {

                    localSource = source;

                    break;

                }

            }

        }

        

        if (localSource) {

            calendar = [EKCalendarcalendarForEntityType:EKEntityTypeEventeventStore:_eventStore];

            calendar.source = localSource;

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

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

            NSError* error;

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

        }

    }

    return calendar;

}

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

[event setCalendar:[self  getCalendar]];

效果如下



iOS 日历插入事件基本功能

请参照

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

这边就不予以赘述了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值