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

user8132109 Source

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.

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.

iphoneswift

Answers

answered 11 months ago Jeni Khant #1

I got name and email of event attendees like this:

for attendee in event.attendees {
    let email = (attendee.url.absoluteString).replacingOccurrences(of: "mailto:", with: "")
    let name = attendee.name ?? ""
}

comments powered by Disqus