Anil Kukadeja
Anil Kukadeja

Reputation: 1358

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)){



            }
}

Upvotes: 4

Views: 4700

Answers (3)

Ajumal
Ajumal

Reputation: 1048

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)" )
                    }
                }


            }
        }

Update : Swift - 4 From BadCode answer.

func getAllContactPhoneNumber() {
let phones: ABMultiValue = ABRecordCopyValue(person,
                                             kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValue
for numberIndex in 0..<ABMultiValueGetCount(phones) {
    let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)
    guard let phoneNumber = phoneUnmaganed!.takeUnretainedValue() as? NSString else {
        return
    }
    let locLabel: NSString = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ?
        ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as NSString: ""
    let cfStr: CFTypeRef = locLabel
    guard let nsTypeString = cfStr as? NSString else {
        return
    }
    let swiftString: String = nsTypeString as String
    let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))
    print("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.

Upvotes: 4

BadCode
BadCode

Reputation: 138

Works for Swift 4 (update of Aju's code). Also edited for SwiftLint.

func getAllContactPhoneNumber() {
    let phones: ABMultiValue = ABRecordCopyValue(person,
                                                 kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValue
    for numberIndex in 0..<ABMultiValueGetCount(phones) {
        let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)
        guard let phoneNumber = phoneUnmaganed!.takeUnretainedValue() as? NSString else {
            return
        }
        let locLabel: NSString = (ABMultiValueCopyLabelAtIndex(phones, numberIndex) != nil) ?
            ABMultiValueCopyLabelAtIndex(phones, numberIndex).takeUnretainedValue() as NSString: ""
        let cfStr: CFTypeRef = locLabel
        guard let nsTypeString = cfStr as? NSString else {
            return
        }
        let swiftString: String = nsTypeString as String
        let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))
        print("Name :-\(swiftString) NO :-\(phoneNumber)" )
    }
}

Upvotes: 0

51d4
51d4

Reputation: 21

Works for Swift 3 (update of Aju's code):

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 {
            print(error?.takeRetainedValue() as Any)
            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: Array<ABRecord> = ABAddressBookCopyArrayOfAllPeople(self.addressBookRef)?.takeRetainedValue() as Array<ABRecord>? {
                // now do something with the array of people

                for record:ABRecord in people {
                let phones : ABMultiValue = ABRecordCopyValue(record,kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValue

                for numberIndex:CFIndex in 0 ... ABMultiValueGetCount(phones)-1
                {
                    let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)
                    let phoneNumber : String = phoneUnmaganed!.takeUnretainedValue() as! String

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

                    //type of phone number
                    let cfStr:CFTypeRef = locLabel
                    let nsTypeString = cfStr as! String
                    let swiftString:String = nsTypeString as String

                    let customLabel = String (stringInterpolationSegment: ABAddressBookCopyLocalizedLabel(locLabel))
                    print("Name \(swiftString) NO \(phoneNumber)" )

                    }
                }


            }
        }

Upvotes: 1

Related Questions