Skip to content

Instantly share code, notes, and snippets.

@bob910078
Created June 17, 2019 03:39
Show Gist options
  • Save bob910078/5af4de0e8031f16324d0e8ca3375ee28 to your computer and use it in GitHub Desktop.
Save bob910078/5af4de0e8031f16324d0e8ca3375ee28 to your computer and use it in GitHub Desktop.
import UIKit
import EventKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let eventStore = EKEventStore()
switch EKEventStore.authorizationStatus(for: .event) {
case .authorized:
insertEvent(store: eventStore)
case .denied:
print("Access Denied")
case .notDetermined:
eventStore.requestAccess(to: .event) { [weak self] (grant, error) in
if grant {
self?.insertEvent(store: eventStore)
} else {
print("Access Denied, \(error.debugDescription)")
}
}
default:
print("Case Default")
}
}
func insertEvent(store: EKEventStore) {
guard let sysCal: EKCalendar = store.defaultCalendarForNewEvents else {
print("fail to accsee default calendar")
fatalError("This may not be failure")
}
let startDate = Date()
let endDate = startDate.addingTimeInterval(2 * 60 * 60) // A TimeInterval value is always specified in seconds
let event = EKEvent(eventStore: store)
event.calendar = sysCal
event.title = "New Event Title"
event.startDate = startDate
event.endDate = endDate
do {
try store.save(event, span: .thisEvent)
} catch {
print("Oh no fail, \(error)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment