0

I'm trying to get the contact details out of the address book on the Mac. I can get the first name and last name fields etc, but I'm struggling with the syntax for ABPersonCopyImageData.

Now according to the documentation ABPersonCopyImageData takes a single parameter of type ABPerson.

Here is my code:

import AddressBook
let thisPerson : ABPerson
let addressBook = ABAddressBook.sharedAddressBook()
rec = addressBook.recordForUniqueId("0005A360-327F-4E12-BBB9-24A842497E12:ABPerson")
let firstName = rec.valueForProperty(kABFirstNameProperty) as! String
let lastName = rec.valueForProperty(kABLastNameProperty) as! String
println("\(firstName) \(lastName)")

let contactImage = ABPersonCopyImageData(thisPerson)

The last line stops the compiler with an error: Cannot invoke 'ABPersonCopyImageData' with an argument list of type (ABPerson). As far as I can tell thisPerson is of type ABPerson. What is going wrong?

iphaaw
  • 6,764
  • 11
  • 58
  • 83

1 Answers1

0

I found out how to do this in ElCapitan:

import Contacts

func getContactImage(name:String) -> NSImage?
{
    let store = CNContactStore()
    do
    {
        let contacts = try store.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName(name), keysToFetch:[CNContactImageDataKey])
        if contacts.count > 0
        {
            if let image = contacts[0].imageData
            {
                return NSImage.init(data: image)
            }
        }
    }
    catch
    {
    }

    return nil
}
iphaaw
  • 6,764
  • 11
  • 58
  • 83