[英]New EventKit EKCalendar will not create?
I am trying to create a new calendar on OS X. I cannot get the calendar (newcalendar) to show in my calendar. 我正在尝试在OS X上创建新日历。我无法使日历(newcalendar)显示在日历中。 I run the code, open my Calendar and it doesn't show. 我运行代码,打开我的日历,它没有显示。 Does anyone have any idea why? 有谁知道为什么吗? Here's what I have in my AppDelegate.swift: 这是我的AppDelegate.swift中的内容:
var store = EKEventStore()
// creating the new calendar
func getCalendar() -> EKCalendar? {
println("5")
let defaults = NSUserDefaults.standardUserDefaults()
if let id = defaults.stringForKey("calendarID") {
return store.calendarWithIdentifier(id)
println("6")
}
else {
var calendar = EKCalendar(forEntityType: EKEntityTypeEvent, eventStore: self.store)
println("6")
calendar.title = "newcalendar"
calendar.color = NSColor.brownColor()
calendar.source = self.store.defaultCalendarForNewEvents.source
println("7")
var error: NSError?
self.store.saveCalendar(calendar, commit: true, error: &error)
println("8")
if error == nil {
defaults.setObject(calendar.calendarIdentifier, forKey: "calendarID")
}
return calendar
println("9")
}
}
// Where it starts
func applicationDidFinishLaunching(aNotification: NSNotification) {
println("1")
var sema = dispatch_semaphore_create(0)
var hasAccess = false
// Ask for permission to access calendars
store.requestAccessToEntityType(EKEntityTypeEvent, completion: { (granted:Bool, error:NSError?) in hasAccess = granted; dispatch_semaphore_signal(sema)})
//println("Permission to connect to Calendar = \(success); error? = \(error)")
println("2")
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)
println("3")
if (!hasAccess){
}
else {
// returns an array of EKCalendars
var eventCalendars =
store.calendarsForEntityType(EKEntityTypeEvent) as [EKCalendar]
println("4")
// for each calendar in array, if the calendar is called newcalendar,
for i in eventCalendars {
if i.title == "newcalendar" {
println("newcalendar already exists")
}
else {
getCalendar()
break
}
}
}
} //end of applicationDidFini...
requestAccessToEntityType
runs asynchronously in a different thread. requestAccessToEntityType
在另一个线程中异步运行。 You need to synch the completion with a semaphore. 您需要将完成与信号量同步。 I do it like this: 我这样做是这样的:
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) {
fatalError....
}
// proceed normally
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.