简体   繁体   English

获取默认日历时出错,仅第一次出现,但此后效果很好

[英]Error getting default calendar, comes only first time but after that works fine

I have written a code to create an event. 我已经编写了创建事件的代码。 Code is pretty straight forward : 代码很简单:

    let eventStore : EKEventStore = EKEventStore()
    eventStore.requestAccess(to: .event, completion: {
            granted, error in
            if (granted) && (error == nil) {
                print("granted \(granted)")
                print("error  \(String(describing: error))")

            let event:EKEvent = EKEvent(eventStore: eventStore)
            event.title = title
            event.startDate = startDate
            event.endDate = endDate
            event.notes = "Lecture"
            event.calendar = eventStore.defaultCalendarForNewEvents
            //                eventStore.saveEvent(event, span: EKSpanThisEvent, error: nil)
            guard ((try? eventStore.save(event, span: EKSpan.thisEvent, commit: true)) != nil)else{
                print("error  saving event")
                completion(false)
                return
            }
            print("Saved Event")
        }}

When the code runs first time it gives this error : 当代码第一次运行时,会出现以下错误:

[EventKit] Error getting default calendar for new events: Error Domain=EKCADErrorDomain Code=1019 "(null)" 2018-05-18 00:18:16.586150+0500 SevimFahrschuleMobile[11413:3650672] [EventKit] No object ID provided. [EventKit]获取新事件的默认日历时出错:错误域= EKCADErrorDomain代码= 1019“(空)” 2018-05-18 00:18:16.586150 + 0500 SevimFahrschuleMobile [11413:3650672] [EventKit]未提供对象ID。 Will not find out if the object exists. 不会发现对象是否存在。

But when I run this code again, it works. 但是,当我再次运行此代码时,它可以工作。 I am not getting. 我没有得到。 There is kind of similar question here but the solution there is to get authorization for reminders and then save event, ie 这里有一个类似的问题,但是解决方案是获得提醒的授权,然后保存事件,即

eventStore.requestAccess(to: .reminder) { (<#Bool#>, <#Error?#>) in
        <#code#>
    }

which sounds totally illogical to me. 这对我来说听起来完全不合逻辑。

Let me know if you see any reason for first time failure. 如果您看到任何首次失败的原因,请告诉我。 Thanks 谢谢

Add the following in Info.plist : Info.plist中添加以下内容:

NSCalendarsUsageDescription NSCalendars用法说明
NSRemindersUsageDescription NSReminders用法说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UICollisionBehavior仅在第一次添加后才能工作 - UICollisionBehavior only works after adding the first time CLLocationManager仅在第一次使用 - CLLocationManager only works first time 得到 ERROR ERROR [object Object] 但仅在 ios 上。 在电脑上工作正常 - Getting ERROR ERROR [object Object] but only on ios. Works fine on computer 仅在物理设备上“尝试添加已经有父节点的 SKNode”,代码中没有错误,在模拟器上工作正常 - Getting 'Attemped to add a SKNode which already has a parent' only on physical device, no error in code, works fine on simulator 应用内购买首次使用沙盒失败,下次可以正常使用 - In app purchase is failing first timewith sandbox , works fine next time APP 只能在 ios 上第一次运行 - APP only works the first time on ios CATransition的ios代码只能在第一次使用? - ios code for CATransition works only the first time? addsubview动画只能在第一次使用 - addsubview animation works only first time Watchkit-didReceiveApplicationContext仅在第一次工作 - Watchkit - didReceiveApplicationContext works only for the first time writeToFile:atomically仅在第一次使用 - writeToFile:atomically only works first time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM