如何在日历中设置默认提醒?

时间:2017-03-15 07:28:46

标签: ios swift swift2 ekevent ekeventstore

尝试向用户的日历添加偶数,但不确定如何设置默认提醒。到目前为止我所拥有的是:

func createEvent(item: Item) {
        let eventStore = EKEventStore()

        eventStore.requestAccessToEntityType(EKEntityType.Event, completion: { (granted, error) in
            if granted && error == nil {
                let event = EKEvent(eventStore: eventStore)
                event.title = item.name
                event.startDate = item.StartDate
                event.endDate = item.endDate
                event.notes = item.description
                event.calendar = eventStore.defaultCalendarForNewEvents

                let alarm1hour = EKAlarm(relativeOffset: 3600) //1 hour
                let alarm1day = EKAlarm(relativeOffset: 86400) //1 day
                event.addAlarm(alarm1day)
                event.addAlarm(alarm1hour)

                // prompt user to add event (to whatever calendar they want)
                let controller = EKEventEditViewController()
                controller.event = event
                controller.eventStore = eventStore
                controller.editViewDelegate = self

                self.presentViewController(controller, animated: true, completion: nil)

            } else {
                self.dismissViewControllerAnimated(true, completion: {})
            }
        })
    }

我认为闹钟=警报,但这似乎没有做任何事情。如何在活动开始前1天设置警报,在事件发生前1小时设置另一个警报?

1 个答案:

答案 0 :(得分:7)

来自EKAlarm(relativeOffset:)的{​​{3}}:

  

负偏移值在事件开始之前触发,而正值在开始之后触发。

我会尝试以下方法:

let alarm1hour = EKAlarm(relativeOffset: -3600) //1 hour
let alarm1day = EKAlarm(relativeOffset: -86400) //1 day

注意:您可能希望在"前一天使用绝对时间"由于夏令时变化引起的警报。