我对EKEventStore完全陌生,并且发现它上周存在。在我保存事件的应用程序中,我希望能够将事件保存到我的手机日历中。如何使用保存的事件数据弹出?
我当前的事件存储控制器类
import UIKit
import EventKit
class EventController: NSObject {
static let sharedInstance = EventController ()
let eventStore = EKEventStore()
override init() {
//grants permission to access calendar
if EKEventStore.authorizationStatusForEntityType(.Event) != (EKAuthorizationStatus.Authorized) {
eventStore.requestAccessToEntityType(.Event, completion: { (granted, error) -> Void in
})
} else {
}
}
//creates event
func createEvent(title: String, startDate: NSDate) {
let eventStore = EKEventStore()
let event = EKEvent(eventStore: eventStore)
if (EKEventStore.authorizationStatusForEntityType(.Event) != EKAuthorizationStatus.Authorized) {
eventStore.requestAccessToEntityType(.Event, completion: { (granted, error) -> Void in
event.title = title
event.startDate = startDate
// event.endDate = endDate
event.calendar = eventStore.defaultCalendarForNewEvents
do {
try eventStore.saveEvent(event, span: .FutureEvents)
} catch {
print("bad things happened")
}
})
}
}
}
从另一个类调用save方法
@IBAction func addToCalendar(sender: AnyObject) {
EventController.sharedInstance.createEvent(event.eventTitle!, startDate: event.eventDate!)
self.dismissViewControllerAnimated(true, completion: nil)
}
谢谢!