问题描述
我正试图迅速从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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!