如何在 EventKit (swift 4) 中添加正确的事件时间?

How to add correct time of event in EventKit (swift 4)?

我遇到了一个小问题,当我实现一个逻辑以每隔一个星期五添加一个事件时。但是,由于某种原因,时间是错误的。默认情况下它始终显示 12:00AM。有没有办法将事件的时间设置为6:30PM?我需要一些帮助来解决这个小问题。我检查了 EventKit 文档。但是卡住了。感谢您的帮助。

import UIKit
import EventKit

class EducationPillarViewController: UIViewController {



     override func viewDidLoad() {
        super.viewDidLoad()

    }


    func addEventToCalendar(title: String, description: String?, startDate: Date, endDate: Date, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) {
        let eventStore = EKEventStore()

        eventStore.requestAccess(to: .event, completion: { (granted, error) in
            if (granted) && (error == nil) {
                let event = EKEvent(eventStore: eventStore)
                event.title = title
                event.startDate = startDate
                event.endDate = endDate
                event.notes = description
                event.calendar = eventStore.defaultCalendarForNewEvents
                do {
                    try eventStore.save(event, span: .thisEvent)

                } catch let e as NSError {
                    completion?(false, e)
                    return

                }
                completion?(true, nil)

            } else {
                completion?(false, error as NSError?)
            }
        })
    }



    @IBAction func MyStudyButtonWasTapped(_ sender: Any) {

        let alertController = UIAlertController(title: "My Study", message:"Every other Fridays at 6:30PM ", preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
        let addtocalendarAction = UIAlertAction(title: "Add to Calendar", style: .default, handler: someHandler)


        alertController.addAction(defaultAction)
        alertController.addAction(addtocalendarAction)
        present(alertController, animated: true, completion: nil)
  }

    func someHandler(alert: UIAlertAction!) {

    let friday = EKRecurrenceDayOfWeek(.friday)


        let dateFormatter = DateFormatter()
        dateFormatter.dateStyle = .short

      let startWeek = Date().startOfWeek
        let endWeek = Date().endOfWeek



   EKRecurrenceRule(recurrenceWith: .weekly, interval: 2, daysOfTheWeek: [friday], daysOfTheMonth: nil, monthsOfTheYear: nil, weeksOfTheYear: nil, daysOfTheYear: nil, setPositions: nil, end: nil)


    addEventToCalendar(title: "My Study", description: "Every Other Fridays", startDate: startWeek!, endDate: endWeek!)


    let alertControlller = UIAlertController(title: "My Study", message: "My Study event has been added to your calendar", preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "ok", style: .cancel, handler: nil)
        alertControlller.addAction(defaultAction)
        self.present(alertControlller, animated: true, completion: nil)


    }




}

extension Date {
    var startOfWeek: Date? {
        let gregorian = Calendar(identifier: .gregorian)
        guard let friday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
        return gregorian.date(byAdding: .day, value: 5, to: friday)
    }

    var endOfWeek: Date? {
        let gregorian = Calendar(identifier: .gregorian)
        guard let friday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
        return gregorian.date(byAdding: .day, value: 5, to: friday)
    }
}

您可以向扩展日期块添加另一个函数

public func setTime(hour: Int, min: Int, sec: Int) -> Date? {
        let x: Set<Calendar.Component> = [.year, .month, .day, .hour, .minute, .second]
        let cal = Calendar.current
        var components = cal.dateComponents(x, from: self)


        components.hour = hour
        components.minute = min
        components.second = sec

        return cal.date(from: components)
    }

然后,一旦你完成了,将声明 startWeek 变量 let startWeek = Date().startOfWeek 的代码行更改为 var startWeek = Date().startOfWeek。然后在该行下方放置 startWeek = startWeek?.setTime(hour: 12, min: 30, sec: 00)

最后更改 endWeek 变量

var endWeek = Date().endOfWeek endWeek = endweek?.setTime(hour: 13, min: 30, sec: 00)