如果更改了endDate,则创建并删除EKEvent

时间:2016-05-02 10:04:40

标签: ios calendar

以下代码有效,完全没问题。

let eventStore = EKEventStore()

                eventStore.requestAccessToEntityType(.Event, completion: { (granted, error) in

                    if granted == false { return }

                    let e = EKEvent(eventStore: eventStore)
                    e.title = self.event!.title

                    let meh = self.event!.commencement
                    print("\(meh)")
                    e.startDate = meh

                    e.location = self.event!.venue.contact.address.assembleAddress()

                    let bleh = NSDate(timeInterval: Double(60*24*60*60), sinceDate: self.event!.commencement)
                    print("\(bleh)")
                    e.endDate = bleh

                    e.calendar = eventStore.defaultCalendarForNewEvents
                    e.URL = self.event!.marketingURL
                    e.addAlarm(EKAlarm(absoluteDate: self.event!.commencement.dateByAddingTimeInterval(Double(-60*60*24*7))))

                    do {
                        try eventStore.saveEvent(e, span: .ThisEvent, commit: true)
                    } catch let error as NSError {
                        print("error \(error)")
                    }

                })

当我将结束日期更改为开始日期之前的8小时时,不会创建事件,或者稍等片刻,然后网络活动指示符会旋转并被删除。

let eventStore = EKEventStore()

                eventStore.requestAccessToEntityType(.Event, completion: { (granted, error) in

                    if granted == false { return }

                    let e = EKEvent(eventStore: eventStore)
                    e.title = self.event!.title

                    let meh = self.event!.commencement
                    print("\(meh)")
                    e.startDate = meh

                    e.location = self.event!.venue.contact.address.assembleAddress()

                    let bleh = NSDate(timeInterval: Double(8*60*60), sinceDate: self.event!.commencement)
                    print("\(bleh)")
                    e.endDate = bleh

                    e.calendar = eventStore.defaultCalendarForNewEvents
                    e.URL = self.event!.marketingURL
                    e.addAlarm(EKAlarm(absoluteDate: self.event!.commencement.dateByAddingTimeInterval(Double(-60*60*24*7))))

                    do {
                        try eventStore.saveEvent(e, span: .ThisEvent, commit: true)
                    } catch let error as NSError {
                        print("error \(error)")
                    }

                })

未打印错误。发生了什么事?

1 个答案:

答案 0 :(得分:0)

日期已过去。确保日期是将来。