应用程序不会将日期保存到日历

时间:2016-08-25 17:10:55

标签: ios swift nsdate nscalendar ekeventstore

我正在尝试将特定日期保存到日历但它不起作用,我使用特定数据并将其更改为字符串 当我使用当前日期工作!但没有特定的。 这是我的代码:

@IBAction func addToCalenderButton(sender: AnyObject) {
    let eventStore = EKEventStore()
    self.event?.Start
    let dateString = "Thu, 24 Oct 2015 07:45"
    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "EEE, dd MMM yyy hh:mm"
    let date:NSDate = dateFormatter.dateFromString(dateString)!
    dateFormatter.dateFormat = "EEE, dd MMM yyy hh:mm"


    if (EKEventStore.authorizationStatusForEntityType(.Event) != EKAuthorizationStatus.Authorized) {
        eventStore.requestAccessToEntityType(.Event, completion: {
            granted, error in
            self.createEvent(eventStore, title: (self.event?.title)!, startDate: date)
        })
    } else {
        createEvent(eventStore, title: (self.event?.title)!, startDate: date)
    }

}

func createEvent(eventStore: EKEventStore, title: String, startDate: NSDate) {
    let event = EKEvent(eventStore: eventStore)

    event.title = title
    event.startDate = startDate
    event.calendar = eventStore.defaultCalendarForNewEvents
    do {
        try eventStore.saveEvent(event, span: .ThisEvent)
        savedEventId = event.eventIdentifier
        Constant.displayAlert(self, title: "", Message: "The event has been added")
    } catch {
       print("Bad things happened")
    }
}

1 个答案:

答案 0 :(得分:0)

你应该改变

dateFormatter.dateFormat = "EEE, dd MMM yyy hh:mm"

dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm"