Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch only mobile numbers in swift using CNContacts?

I have some code to retrieve all the phone numbers in the users contacts, but would like to filter out only mobile numbers. Currently, I am just doing this by only adding numbers with a first digit of "+" or second digit of "7" to an array, as shown below:

    func findContacts () -> [CNContact]{
    let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),CNContactPhoneNumbersKey]
    let fetchRequest: CNContactFetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)
    var contacts = [CNContact]()
    CNContact.localizedStringForKey(CNLabelPhoneNumberiPhone)
    fetchRequest.mutableObjects = false
    fetchRequest.unifyResults = true
    fetchRequest.sortOrder = .UserDefault

    let contactStoreID = CNContactStore().defaultContainerIdentifier()

    do {

        try CNContactStore( ).enumerateContactsWithFetchRequest(fetchRequest) { (let contact, let stop) -> Void in
            if contact.phoneNumbers.count > 0 {
                contacts.append(contact)
            }
            if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
                for phoneNumber:CNLabeledValue in contact.phoneNumbers {
                    let number = phoneNumber.value as! CNPhoneNumber
                    print(number.stringValue)
                    let index = number.stringValue.startIndex.advancedBy(1)
                    let indexPlus = number.stringValue.startIndex.advancedBy(0)
                    if number.stringValue[index] == Character(String(7)) || number.stringValue[indexPlus] == Character("+"){
                        self.allNumbers.append("\(number.stringValue)")
                    }
                }
            }
        }

Since contacts are stored on an iPhone with a "mobile" label, I was wondering if only these numbers could be added to the array. Thanks :)

like image 335
R_McVities Avatar asked May 04 '16 22:05

R_McVities


People also ask

How do I get contacts in Swift?

In the Custom Class section change the class to TableViewController. Add an empty Swift file to the project, name it FetchedContact. swift and add the following code. The properties of the FetchContact struct will be used to display the contacts in the table view.

How do I sync contacts in Swift?

Create a new swift file and give it a name “ContactUtility. swift”, follow the same steps as we created CoreDataManager class. Here we will use Contacts library to access contact book, so import contacts and ContactUI framework in the Link Binary with Library of the build Phase section in the project setting.


1 Answers

Check if the number's label is mobile like this:

var mobiles = [CNPhoneNumber]()

for num in contact.phoneNumbers {
    let numVal = num.value as! CNPhoneNumber
    if num.label == CNLabelPhoneNumberMobile {
        mobiles.append(numVal)
    }
}

Then you have an array of mobile phone numbers for that person.

like image 93
brimstone Avatar answered Oct 18 '22 19:10

brimstone