【问题标题】:Using EventKit with swift on OSX在 OSX 上快速使用 EventKit
【发布时间】:2015-04-02 12:04:12
【问题描述】:

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

【问题讨论】:

  • 您可以尝试使用 EventKit 中的 EKEventStore 来执行此操作,有一个函数 predicateForEventsWithStartDate:endDate:calendars: 返回一个 Date 谓词,您可以将其与 enumerateEventsMatchingPredicate:usingBlock: 一起使用以获取 7 中的所有事件天跨度

标签: xcode macos swift


【解决方案1】:

我使用以下单例访问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 是我创建警报的私有方法之一。根据需要更换它。

要访问我使用的应用程序中的事件存储区

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

关于 then event store 的文档非常完整,因此您可能可以从这里开始。

【讨论】:

  • 感谢这个非常好的解释。我想我会从这里找到路。
猜你喜欢
  • 2015-05-22
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 2020-10-04
  • 1970-01-01
相关资源
最近更新 更多