在Ekevent中设置选定的时间



在我的应用程序中,我可以选择EKEvent的日期和时间。除了时间,我几乎取得了想要的东西。我该如何设置时间。以下是我的代码,我尝试过。

func setReminder(){
    let eventStore = EKEventStore()
    eventStore.requestAccess(
        to: EKEntityType.event, completion: {(granted, error) in
            if !granted {
                print("Access to store not granted")
                print(error!.localizedDescription)
            } else {
                print("Access granted")
                let event = EKEvent(eventStore: eventStore)
                event.title = self.getCategoryText + " Reminder"
                event.calendar =
                    eventStore.defaultCalendarForNewEvents
                event.timeZone = TimeZone.current
                let alarm = EKAlarm(absoluteDate: self.selectedDate!)
                event.addAlarm(alarm)
                if self.daysBeforeDate != nil{
                    let earlierDate = self.daysBeforeDate
                    let earlierAlarm = EKAlarm(absoluteDate: earlierDate!)
                    event.addAlarm(earlierAlarm)
                }
                print("selected time", self.selectedTime)
                var number1: Double = (self.selectedTime! as NSString).doubleValue
                event.startDate = self.selectedDate!.addingTimeInterval(number1)
                event.endDate = self.selectedDate!
                do {
                    try eventStore.save(event, span: .thisEvent, commit: true)
                } catch let error  {
                    print("Reminder failed with error (error.localizedDescription)")
                    return
                }
            }
    })
}

更新1

let calendar = Calendar.current
  var dateComponents = DateComponents()
  dateComponents.year = calendar.component(.year, from: self.selectedDate!)
  dateComponents.month = calendar.component(.month, from: self.selectedDate!)
  dateComponents.day = calendar.component(.day, from: self.selectedDate!)
  dateComponents.timeZone = TimeZone.current
  dateComponents.hour = self.selectedHour
  dateComponents.minute = self.selectedMinute
  let userCalendar = Calendar.current // user calendar
  let someDateTime = userCalendar.date(from: dateComponents)

这是我用于创建一个时间间隔的事件的代码,对我来说很好:

func saveEvent(on date: Date, withTitle title: String, forHourse hourse: Int) {
    let event = EKEvent(eventStore: eventStore!)
    event.title = title
    event.startDate = date
    event.endDate = date.addingTimeInterval((TimeInterval(hourse * 60 * 60)))
    let alarm = EKAlarm(absoluteDate: date)
    event.alarms = [alarm]
    event.calendar = (eventStore?.defaultCalendarForNewEvents)!
    do {
        try eventStore?.save(event, span: .thisEvent, commit: true)
    } catch {
    }
}

相关内容

  • 没有找到相关文章

最新更新