Modifying EKParticipants (attendees) in EventKit like Sunrise

前端 未结 2 398
渐次进展
渐次进展 2020-12-05 07:48

My goal is to add some invitees to an EKEvent. I\'ve looked into other questions like this, such as this one, but all say that it is essentially impossible to a

2条回答
  •  臣服心动
    2020-12-05 08:11

    Swift version of rebello95's solution. I can confirm it works like a charm! I played around with the firstName and lastName properties but they do not seem to matter at all, so I left them out of my example. Only the emailAddress matters, which is matched against your contact list to display a full name in for example the native calendar app.

    private func createEventWithAttendees() {
        let eventStore = EKEventStore()
    
        let calendars = eventStore.calendarsForEntityType(.Event)
    
        let event = EKEvent(eventStore: eventStore)
        event.title = "TEST EVENT"
        event.startDate = NSDate()
        event.endDate = NSDate().dateByAddingTimeInterval(60 * 60)
        event.calendar = calendars[0]
    
        var attendees = [EKParticipant]()
        for i in 0 ..< 5 {
            if let attendee = createParticipant(email: "test\(i)@email.com") {
                attendees.append(attendee)
            }
        }
        event.setValue(attendees, forKey: "attendees")
    
        try! eventStore.saveEvent(event, span: .ThisEvent)
    }
    
    private func createParticipant(email email: String) -> EKParticipant? {
        let clazz: AnyClass? = NSClassFromString("EKAttendee")
        if let type = clazz as? NSObject.Type {
            let attendee = type.init()
            attendee.setValue(email, forKey: "emailAddress")
            return attendee as? EKParticipant
        }
        return nil
    }
    

提交回复
热议问题