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)
}
}
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).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With