Swift 4.0 Eventkit Cannot get calendars and events correctly

后端 未结 3 578
后悔当初
后悔当初 2021-02-06 14:55

I\'m trying to use Eventkit to access Mac Calendar. Access is successfully requested but I keep getting nil or an empty array of calendar or events, even though I have several c

相关标签:
3条回答
  • 2021-02-06 15:09

    Try this method

    Create and Get Calendar

    func getCalendar() -> EKCalendar? {
            let defaults = UserDefaults.standard
        
            if let id = defaults.string(forKey:"calendarID") {
                return eventStore.calendar(withIdentifier: id)
              } else {
                let calendar = EKCalendar(for: .event, eventStore: eventStore)
        
                   calendar.title = "Calendar Title"
                   calendar.cgColor = UIColor.blue
                   calendar.source = self.eventStore.defaultCalendarForNewEvents!.source
        
                 do {
                     try eventStore.saveCalendar(calendar, commit: true)
                     defaults.set(calendar.calendarIdentifier, forKey: "calendarID")
        
                     print("Created calander")
                   } catch let error as NSError {
                   print("failed to Create calendar with error : \(error)")
                   }
                
        
                  return calendar
              }
          }
    
    0 讨论(0)
  • 2021-02-06 15:17

    After lots of trial and error, I found the answer.

    You need to set the com.apple.security.personal-information.calendars key to YES in your entitlements file, even if your app is not sandboxed. There is a bug in Apple's implementation of EventKit that prevents your app getting access to calendars if it does not set this key, even if the sandbox is disabled.

    0 讨论(0)
  • 2021-02-06 15:17

    I might be wrong but you are missing one parameter in your predicate you are passing in nil so there is nothing to sort essentially no output. Try changing your code to:

     let sources = eventStore.sources
        for source in sources{
            print(source.title)
            for calendar in source.calendars(for: .event){
                print(calendar.title)
            }
        }
    
        let calendars = eventStore.calendars(for: .event)
        let predicate = self.eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: calendars)  //change here
        let events = self.eventStore.events(matching: predicate)
        print(calendars)
        print(events)
    
    0 讨论(0)
提交回复
热议问题