Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the contact number in ios 9

in ios 8, i used the below code to print the user's contact Number,

if let contacts = ABAddressBookCopyArrayOfAllPeople(self.addressBookRef)?.takeRetainedValue() as? NSArray {
            for record:ABRecordRef in contacts {

                let phones:ABMultiValueRef = ABRecordCopyValue(record, kABPersonPhoneProperty).takeUnretainedValue() as ABMultiValueRef
                for(var numberIndex : CFIndex = 0; numberIndex < ABMultiValueGetCount(phones); numberIndex++)
                {
                    let phoneUnmaganed = ABMultiValueCopyValueAtIndex(phones, numberIndex)
                    let phoneNumber : String = phoneUnmaganed.takeUnretainedValue() as! String

println(phoneNumber) } }

But apple introduced new contact framework in ios 9. Now i stuck with retrieving contact number. I found some code in the apple site and in other sites as given below, But still it's not exactly printing only the contact numbers,

contacts = try store.unifiedContactsMatchingPredicate(
                CNContact.predicateForContactsMatchingName("Siva"),
                keysToFetch:[CNContactPhoneNumbersKey])
            for contact:CNContact in contacts {
                if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
                    for phoneNumber:CNLabeledValue in contact.phoneNumbers {
                        print(phoneNumber.value)
                    }
                }
like image 404
Arun V Avatar asked Sep 21 '15 12:09

Arun V


People also ask

How do you find phone numbers on iPhone contacts?

Find a contactTap the search field at the top of the contacts list, then enter a name, address, phone number, or other contact information. You can also search your contacts using Search (see Search from the iPhone Home Screen or Lock Screen).

How do I manage contacts on iPhone?

Go to Settings > Contacts > Accounts. Tap the account that has contacts that you want to add or remove. To add contacts, turn on Contacts. To remove contacts, turn off Contacts, then tap Delete from My iPhone.

How do you select all contacts on iPhone?

Press and hold Ctrl and select the contacts you want to delete. As long as you hold down the Ctrl key, you can select multiple contacts. Selected contacts will highlight in blue so you'll know who you've clicked. Ctrl + click a selected contact to de-select them.


1 Answers

This should do it.

        if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
            for phoneNumber:CNLabeledValue in contact.phoneNumbers {
                let a = phoneNumber.value as! CNPhoneNumber
                print("\(a.stringValue)")
            }
        }

prints in the style of (555) 766-4823

like image 170
Mathias Navne Avatar answered Nov 23 '22 19:11

Mathias Navne