AddressBook: ABRecordCopyValue(person, kABPersonPhoneProperty) failing to get some numbers

467 Views Asked by At

After much searching, I'm stuck with a problem that (I believe) hasn't been clearly addressed.

My goal is to pull phone numbers from the phone's contact list. Permission has already been granted. I use this bit of code to extract data from a given contact (variable 'person'):

let unmanagedPhones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeUnretainedValue()
println("unmanagedPhone: \(unmanagedPhones)")

For some of the contacts on my phone, this returns the following (I've censored some of the numbers with #'s):

unmanagedPhone: ABMultiValueRef 0x170468d00 with 2 value(s) 0: $!Mobile!$ (0x#########) - 1 (###) ###-#### (0x170#45###) 1: $!Work!$ (0x#########) - 1 (###) ###-#### (0x#70##3#a0)

For many others, though, this returns:

unmanagedPhone: ABMultiValueRef 0x170474a40 with 0 value(s)

I've confirmed that the contacts in question do have at least one number. In exploring the address book, I can't figure out why some contacts work and others don't. Does anyone have any advice or direction as to how to begin to debug this? Thanks, all!

Using Swift on Xcode 6.4, iOS 8.

1

There are 1 best solutions below

1
Paul C On

Try this:

let unmanagedPhones = ABRecordCopyValue(person, kABPersonPhoneProperty)

    //check to make sure we have phone numbers or we will get Fatal error
    if unmanagedPhones != nil {

        let phones: ABMultiValueRef = unmanagedPhones.takeUnretainedValue() as ABMultiValueRef
        let allPhones = ABMultiValueCopyArrayOfAllValues(phones).takeRetainedValue() as NSArray

        for eachPhone in allPhones{
            print(eachPhone)
        }
    }
    else {
        println("No phone numbers found")
    }