Silve2611
Silve2611

Reputation: 2278

Using EventKit with swift on OSX

I'm trying to get a list of all events from iCal for this week with swift. I would like to see how many Events there are and what categories they belong to. How can this be done programatically. Is Eventkit the right choice or should AppleScript be used? Is there a tutorial for Eventkit with swift? I could not find one.

Upvotes: 0

Views: 2490

Answers (1)

qwerty_so
qwerty_so

Reputation: 36313

I use the following singleton to access the EventStore

private let _SingletonSharedInstance = EventStore()

class EventStore {
  let eventStore = EKEventStore ()

  class var sharedInstance : EventStore {
    return _SingletonSharedInstance
  }

  init() {
    var sema = dispatch_semaphore_create(0)
    var hasAccess = false

    eventStore.requestAccessToEntityType(EKEntityTypeEvent, completion: { (granted:Bool, error:NSError?) in hasAccess = granted; dispatch_semaphore_signal(sema) })

    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
    if (!hasAccess) {
      alert ("ACCESS", "ACCESS LONG")
      let sharedWorkspace = NSWorkspace.sharedWorkspace()
      sharedWorkspace.openFile("/Applications/System Preferences.app")
      exit (0)
    }
  }
}

Note: alert is one of my private methods to create an alert. Replace it as needed.

To access the eventstore in my app I use

let eventStore = EventStore.sharedInstance.eventStore
...
for et in eventStore.calendarsForEntityType(EKEntityTypeEvent) {
   // check calendars
}

The docu about then event store is quite complete so you can probably make your way from here.

Upvotes: 5

Related Questions