gpt4 book ai didi

ios - 新的 EventKit EKCalendar 不会创建?

转载 作者:行者123 更新时间:2023-11-28 07:05:09 24 4
gpt4 key购买 nike

我正在尝试在 OS X 上创建一个新日历。我无法在我的日历中显示日历 (newcalendar)。我运行代码,打开我的日历,但它没有显示。有谁知道为什么?这是我的 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 在不同的线程中异步运行。您需要将完成与信号量同步。我这样做:

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

关于ios - 新的 EventKit EKCalendar 不会创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30853099/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com