ios - 新的 EventKit EKCalendar 不会创建?

标签 ios macos swift icalendar eventkit

我正在尝试在 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/

相关文章:

ios - AFNetworking 在 404 Bad Request 仍然获得 JSON 响应后获得 JSON

iphone - UIWebView 中嵌入的电影播放器​​确实在 iPhone 上进入了全屏,但在 iPad 上没有

python - Matplotlib PDF 导出使用了错误的字体

swift - 可以在 Swift 中将枚举类型名称作为参数传递吗?

iphone - 以编程方式旋转 Apple Watch 显示屏

ios - 如何使用 Swift 提高定时器对象的速度

ios - Swift - UITableView 不适用于委托(delegate)

macos - 设置launchctl limit maxfiles 512无限导致错误: Neither the hard nor soft limit for "maxfiles" can be unlimited

iphone - 在 Xcode 中,info.plist 中的 Bundle 名称不会更改包名称(.app 文件名)

Swift Segmented Control 默认状态