在OSX上快速使用EventKit [英] Using EventKit with swift on OSX

查看:128
本文介绍了在OSX上快速使用EventKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图迅速从iCal获取本周所有事件的列表. 我想看看有多少个事件以及它们属于什么类别. 如何以编程方式完成此操作. Eventkit是正确的选择还是应该使用AppleScript? swift是否有适用于Eventkit的教程?我找不到一个.

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.

推荐答案

我使用以下单例访问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)
    }
  }
}

注意:alert是我创建警报的私有方法之一.根据需要更换.

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.

这篇关于在OSX上快速使用EventKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆