anil kukdeja anil kukdeja - 7 months ago 23
Swift Question

How to fetch custom label phone numbers in address book swift

I am trying to fetch custom label phone number's using address book (swift).

I have tried with kABOtherLabel property but i did not got desired result.

I would like to know is there any way to fetch custom label properties..?

Here i am sharing what i am doing currently.

Thanks in advance.

//phone


var phones : ABMultiValueRef = ABRecordCopyValue(contactRef,kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValueRef

for(var numberIndex : CFIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
{

// Number in contact details of current index

let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)


let phoneNumber : NSString = phoneUnmaganed.takeUnretainedValue() as! NSString

// Label of Phone Number

let locLabel : CFStringRef = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ? ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as CFStringRef : ""

//check for home
if (String(locLabel) == String(kABHomeLabel))
{
contact.sUserTelHome = phoneNumber as String
contact.sUserTelHomeTrim = contact.sUserTelHome?.trimmedContactNumber()

}

//check for work
else if (String(locLabel) == String(kABWorkLabel))
{
contact.sUserTelWork = phoneNumber as String
contact.sUserTelWorkTrim = contact.sUserTelWork?.trimmedContactNumber()

}

//check for mobile
else if (String(locLabel) == String(kABPersonPhoneMobileLabel))
{
contact.sUserTelMobile = phoneNumber as String
contact.sUserTelMobileTrim = contact.sUserTelMobile?.trimmedContactNumber()
}

else if(String(locLabel) == String(kABOtherLabel)){



}
}

Aju Aju
Answer
let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))

This will print the label of phone number. I believe this is what you looking, For more details Please visit here. Find the full code below.

EDIT

let status = ABAddressBookGetAuthorizationStatus()
        if status == .Denied || status == .Restricted {
            // user previously denied, to tell them to fix that in settings
            return
        }

        // open it

        var error: Unmanaged<CFError>?
        let addressBook: ABAddressBook? = ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue()
        if addressBook == nil {
            println(error?.takeRetainedValue())
            return
        }

        // request permission to use it

        ABAddressBookRequestAccessWithCompletion(addressBook) {
            granted, error in

            if !granted {
                // warn the user that because they just denied permission, this functionality won't work
                // also let them know that they have to fix this in settings
                return
            }

            if let people = ABAddressBookCopyArrayOfAllPeople(addressBook)?.takeRetainedValue() as? NSArray {
                // now do something with the array of people

                for record:ABRecordRef in people {
                    var phones : ABMultiValueRef = ABRecordCopyValue(record,kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValueRef

                    for(var numberIndex : CFIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
                    {
                        let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)


                        let phoneNumber : NSString = phoneUnmaganed.takeUnretainedValue() as! NSString

                        let locLabel : CFStringRef = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ? ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as CFStringRef : ""

                        var cfStr:CFTypeRef = locLabel
                        var nsTypeString = cfStr as! NSString
                        var swiftString:String = nsTypeString as String

                        let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))


                        println("Name :-\(swiftString) NO :-\(phoneNumber)" )
                    }
                }


            }
        }

OutPUT

Name :-_$!<Mobile>!$_ NO :-8592-841222
Name :-CUSTOMLABEL NO :-111
Name :-_$!<Home>!$_ NO :-45445

Middle one is my customised label,

Please note that default label always start with _$!< characters.

Comments