1

我终于有时间回去做这件事了。下面的代码提取地址标签和 ID。身份证没问题。问题是当我打印地址标签时它给了我这个:

_$!<Home>!$_

这是正常的还是我的代码中有什么我可以更改来修复它?我想要它包含的只是“家”。在进入数组之前,我是否需要去掉第一个和最后四个字符?

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)")
        }

如何检查一个联系人是否有多个邮政地址(家庭、工作等)?如果有多个 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)
        }
    }
4

1 回答 1

1

您可以使用以下代码获取多个 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)")
        }
    }
于 2020-02-07T12:16:16.347 回答