1

I finally got time to go back and work on this. The code below pulls the address labels and IDs. The IDs are fine. The problem is when I print theAddressLabel it give me this:

_$!<Home>!$_

Is this normal or is there something in my code I can change to fix it? All I want it to contain is "Home". Do I need to strip-out the first and last four characters before it goes into the array?

func getContactFromID_Ouote2(contactID: String)
    {
        struct contactAddresses
        {
            var theLabel: String
            var theID: String
        }

        let store = CNContactStore()
        var theName = CNContact()
        var theTypes = [contactAddresses]()
        var theAddressID: String = ""
        var theAddressLabel: String = ""

        let theKeys = [CNContactPostalAddressesKey] as [CNKeyDescriptor]

        do {
            theName = try store.unifiedContact(withIdentifier: contactID, keysToFetch: theKeys)

            let postalAddress = theName.postalAddresses
            postalAddress.forEach { (mailAddress) in
                theTypes.append(contactAddresses(theLabel: mailAddress.label!, theID: mailAddress.identifier))

            }

            for theItem in theTypes
            {
                theAddressLabel = theItem.theLabel
                theAddressID = theItem.theID
                print(theAddressLabel)
                print(theAddressID)
            }

        } catch {
            print("Fetching contact data failed: \(error)")
        }

How do I check to see if there are multiple postalAddresses (home, work, etc.) for a contact? If there are multiple postalAddresses then my plan is to present an alert to allow the user to pick the one to use. Presenting the alert isn't a problem, I just need help with getting the addresses.

Thanks in advance.

func getContactFromID_Ouote(contactID: String)
    {
        let store = CNContactStore()
        var theName = CNContact()

        let theKeys = [CNContactNamePrefixKey,
                       CNContactGivenNameKey,
                       CNContactFamilyNameKey,
                       CNContactNameSuffixKey,
                       CNContactOrganizationNameKey,
                       CNContactPostalAddressesKey,
                       CNContactFormatter.descriptorForRequiredKeys(for: .fullName)] as! [CNKeyDescriptor]

        do {
            theName = try store.unifiedContact(withIdentifier: contactID, keysToFetch: theKeys)

            contactName = CNContactFormatter.string(from: theName, style: .fullName)!

            contactPrefix = theName.namePrefix
            contactFirst = theName.givenName
            contactLast = theName.familyName
            companyName = theName.organizationName == "" ? "" : theName.organizationName

        } catch {
            print("Fetching contact data failed: \(error)")
        }

        if let firstPostalAddress = (theName.postalAddresses.first),
            let labelValuePair = firstPostalAddress.value(forKey: "labelValuePair") as? AnyObject,
            let finalPostalAddress = labelValuePair.value(forKey: "value") as? CNPostalAddress
        {
            mailAddress = CNPostalAddressFormatter.string(from: finalPostalAddress, style: .mailingAddress)
        }
    }
Quailcreek
  • 125
  • 2
  • 9

1 Answers1

1

Your can use below code for fetch multiple mailingAddresses.

func getContactFromID_Ouote(contactID: String)
    {
        let store = CNContactStore()
        var theName = CNContact()

        let theKeys = [CNContactEmailAddressesKey] as [CNKeyDescriptor]

        do {
            theName = try store.unifiedContact(withIdentifier: contactID, keysToFetch: theKeys)

            let emailAddress = theName.emailAddresses
            emailAddress.forEach { (mailAddress) in
                print("Your Mail Address is :- ",mailAddress.value)
                print("Your Mail Type :- ",mailAddress.label)
            }
        } catch {
            print("Fetching contact data failed: \(error)")
        }
    }
Nikunj5294
  • 391
  • 3
  • 14
  • Hi, Thanks for the reply. I'm actually looking for postalAddresses. I think I can make your example work thought. – Quailcreek Feb 08 '20 at 02:50