Fetching all contacts in ios Swift?

后端 未结 7 1396
天命终不由人
天命终不由人 2020-11-30 20:00

I am aware of the ios swift has a Contacts Framework where I can fetch contacts, but I cannot find any method to fetch all the contacts together where I can access each of t

7条回答
  •  自闭症患者
    2020-11-30 20:12

    Please see my answer it is based on answers above with certain improvements, just do the following

    In your pod file

    source 'https://github.com/CocoaPods/Specs.git'
    pod 'PhoneNumberKit', '~> 2.6'
    

    then run pod install

    then in your ViewController File

    import Contacts
    import PhoneNumberKit
    import UIKit
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let contactStore = CNContactStore()
        var contacts = [CNContact]()
        let keys = [
            CNContactFormatter.descriptorForRequiredKeys(for: .fullName),
            CNContactPhoneNumbersKey,
            CNContactEmailAddressesKey,
        ] as [Any]
        let request = CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])
        do {
            try contactStore.enumerateContacts(with: request) {
                contact, _ in
                // Array containing all unified contacts from everywhere
                contacts.append(contact)
                for phoneNumber in contact.phoneNumbers {
                    if let number = phoneNumber.value as? CNPhoneNumber, let label = phoneNumber.label {
                        let localizedLabel = CNLabeledValue.localizedString(forLabel: label)
    
                        // Get The Name
                        let name = contact.givenName + " " + contact.familyName
                        print(name)
    
                        // Get The Mobile Number
                        var mobile = number.stringValue
                        mobile = mobile.replacingOccurrences(of: " ", with: "")
    
                        // Parse The Mobile Number
                        let phoneNumberKit = PhoneNumberKit()
    
                        do {
                            let phoneNumberCustomDefaultRegion = try phoneNumberKit.parse(mobile, withRegion: "IN", ignoreType: true)
                            let countryCode = String(phoneNumberCustomDefaultRegion.countryCode)
                            let mobile = String(phoneNumberCustomDefaultRegion.nationalNumber)
                            let finalMobile = "+" + countryCode + mobile
                            print(finalMobile)
                        } catch {
                            print("Generic parser error")
                        }
    
                        // Get The Email
                        var email: String
                        for mail in contact.emailAddresses {
                            email = mail.value as String
                            print(email)
                        }
                    }
                }
            }
    
        } catch {
            print("unable to fetch contacts")
        }
    }
    

提交回复
热议问题