正在检查联系人swift 5的多个postalAddress

问题描述 投票:0回答:1

我如何检查联系人是否有多个postalAddress(住所,工作地点等)?如果有多个postalAddresses,那么我的计划是提出一个警报,以允许用户选择要使用的一个。发出警报不是问题,我只需要获得地址的帮助。

提前感谢。

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)
        }
    }
ios13 swift5 xcode11.3
1个回答
0
投票

您可以使用下面的代码来获取多个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)")
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.