How to get the list of attendees and their email addresses from calendar's event in Swift?

I want to get the attendees list of the calendar's event, and I dont know how to do it in Swift 3. I also want to get the email address of any event's attendee to contact them through my app. Does anyone can help me?

Thanks.

Look at the EventKit doc:


h ttps://developer.apple.com/documentation/eventkit/ekcalendaritem


You get the list of participants with


var attendees: [EKParticipant]?

The attendees associated with the calendar item, as an array of EKParticipant objects.


For the email, have a look here :

h ttps://stackoverflow.com/questions/13642786/how-to-get-ekevent-ekparticipant-email

Thank you Claude31 for your answer.


I tried this solution : var attendees: [EKParticipant]? = event?[(indexPath as! INDexPath).row].attendees

but its always return "nil" event when the en event has participants.


For the email, i had a look at :

https://stackoverflow.com/questions/13642786/how-to-get-ekevent-ekparticipant-email


but nothing works.

How do you retrieve the event ?


What do you get inside the event ?

I retrieve the events with this fuction :

func loadEvents() {

let dateFormatter = DateFormatter()

dateFormatter.dateFormat = "dd-MM-yyyy"

let date = Date()

let calendar2 = Calendar.current

let components = calendar2.dateComponents([.year, .month, .day], from: date)

let year = components.year

let month = components.month

let day = components.day

let startDate = dateFormatter.date(from: "\(day ?? 01)" + "-" + "\(month ?? 01)" + "-" + "\(year ?? 2017)")

let endDate = dateFormatter.date(from: "31-12-" + "\(year ?? 2099)")

if let startDate = startDate, let endDate = endDate {

let eventStore = EKEventStore()

let eventsPredicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: [calendar])

self.events = eventStore.events(matching: eventsPredicate).sorted() {

(e1: EKEvent, e2: EKEvent) -> Bool in

return e1.startDate.compare(e2.startDate) == ComparisonResult.orderedAscending

}

}

}


And i list them inside a UITableView like this :

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell")!

cell.textLabel?.text = events?[(indexPath as NSIndexPath).row].title

cell.detailTextLabel?.text = formatDate(events?[(indexPath as NSIndexPath).row].startDate)

return cell

}



But when i'm trying to get attendees for one event like this:

if (events?[(indexPath as IndexPath).row].hasAttendees)! {

let eventsAttendees: [EKParticipant]! = events?[(indexPath as IndexPath).row].attendees

}


i always have eventsAttendees = nil


i dont understand.

So I understand events is a property defined in the class. Exact ?


Check it is not defined several times, with a new var declaration.

Then could you print a test in func loadEvents(), to check if it is loaded properly:


        if let startDate = startDate, let endDate = endDate {
            let eventStore = EKEventStore()
   
            let eventsPredicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: [calendar])
   
            self.events = eventStore.events(matching: eventsPredicate).sorted() {
                (e1: EKEvent, e2: EKEvent) -> Bool in
                return e1.startDate.compare(e2.startDate) == ComparisonResult.orderedAscending
            }

 Swift.print(self.events)

The events are loading properly when i try the app on a reel device.

The events are loading properly when i try the app on a reel device.

Does that mean the app works on device and not on simulator ?


A few more questions:

- Did you try to print(self.events) ; what do you get ?

- Does tableView display the events correctly ? (I guess no)

Hello Claude31,

Thank you for your answer.


The app work on simulator and on a reel device.


I printed self.events, i have all the events programing on a device.

tableView displays the events correctly.

I found one way to retreive mail addresse of the attendees like this:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell")!

cell.detailTextLabel?.text = ""

cell.textLabel?.text = events?[(indexPath as NSIndexPath).row].title + "-" + formatDate(events?[(indexPath as NSIndexPath).row].startDate

if (events?[(indexPath as NSIndexPath).row].hasAttendees) {

let eventsAttendees = events?[(indexPath as NSIndexPath).row].attendees

for person in eventsAttendees {

cell.detailTextLabel?.text = "\(person.url.description); \(cell.detailTextLabel?.text)"

}

}


return cell

}


But the amail address always bigins with "mailto:" and i want to remove it.

Get the URL as a string and remove the mailto: part.

let emailLink = person.url?.absoluteString ?? ""
let email = emailLink.replacingOccurrences(of: "mailto:", with: "")
cell.detailTextLabel?.text = "\(email); \(cell.detailTextLabel?.text)"
How to get the list of attendees and their email addresses from calendar's event in Swift?
 
 
Q