无法在iPhone上创建日历,但可以在模拟器上创建

时间:2015-02-03 18:05:50

标签: ios xcode swift calendar

我想创建一个自定义日历,在其中创建所有与应用相关的事件。我希望此日历属于EKSourceType Local

我可以在模拟器上创建这样的日历并向其添加事件但是当我尝试在iPhone上添加它时它确实无法正常工作。

我创建日历的代码:

    if (eventManager.eventStore?.calendarWithIdentifier(NSUserDefaults.standardUserDefaults().objectForKey("calendar_identifier") as? String) == nil){
        //The calendar doesn't exist, create a new one with the correct properties.
        calendar = EKCalendar(forEntityType: EKEntityTypeEvent, eventStore: eventManager.eventStore)
        calendar.title = "My Calendar"
        calendar.CGColor = UIColor(red: 0, green: 0.62, blue: 0.10, alpha: 1).CGColor
        // Find the proper source type value.
        var localSource:EKSource? = nil

        if let eventStore = eventManager.eventStore{
        //get the correct source for the calendar
            for source in eventStore.sources() as [EKSource] {
                println("eventStore.source - \(source)")
                switch (source.sourceType.value){
                /*
                case (EKSourceTypeSubscribed.value):
                  localSource = source
                */
                case (EKSourceTypeLocal.value):
                  localSource = source
                default:
                  break
                }
            }
            calendar.source = localSource
            }
            //save the newly created calendar
            eventManager.eventStore!.saveCalendar(calendar, commit: true, error: &error)
            //check for any errors.
            if(error == nil){
                //no errors were encountered save identifier
                NSUserDefaults.standardUserDefaults().setObject(calendar.calendarIdentifier, forKey: "calendar_identifier")
                    println("Making calendar")
            }else{
                    println("error in making calendar - \(error?.localizedDescription)")
                    return
            }
       }

我首先检查NSUserDefaults中是否存在日历标识符。然后,我创建新日历,并在找到后为其分配本地源。然后我保存日历(工作并返回true)。没有错误返回,当我打印源它给出:

EKSource <0x1700dad30> {UUID = BD42B102-A185-4A93-9114-737001A4C408; type = Local; title = Default; externalID = (null)}

我在这里做错了什么,或者更好的方法是什么?

1 个答案:

答案 0 :(得分:0)

这似乎是iCloud的错误/奇怪行为。

如果您已激活iCloud,但您已禁用同步提醒,则会发生这种情况。 This Guy遇到了同样的问题(但无法解决)。

尝试启用提醒同步。