【问题标题】:Fetching a contacts mailing address using the address label swift 5使用地址标签 swift 5 获取联系人邮寄地址
【发布时间】:2020-04-25 04:47:28
【问题描述】:

如何使用邮寄地址标签提取联系人邮寄地址?

下面的函数 buildContactsAddress_Array 构建一个包含地址标签(名称)和地址 ID 的数组。该数组用于填充 tableView,用户可以在其中通过名称选择地址。我已经包含了几乎所有相关的代码,以尽量让事情变得清晰。提前致谢。

这是我要更改或替换以使用地址标签的部分。现在它只使用第一个/家庭地址。

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)

}

struct contactAddresses
{
    var theLabel: String
    var theID: String
}

private var addressesArray = [contactAddresses]()
private var addressID: String = ""
private var theContactID: String = ""

此函数使用联系人 ID 提取联系人信息。

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

        let theKeys = [CNContactNamePrefixKey,
                       CNContactGivenNameKey,
                       CNContactFamilyNameKey,
                       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? NSObject,
            let finalPostalAddress = labelValuePair.value(forKey: "value") as? CNPostalAddress
        {
            mailAddress = CNPostalAddressFormatter.string(from: finalPostalAddress, style: .mailingAddress)
        }
    }

此函数将联系人地址放入数组中。然后使用该数组来填充 tableView。

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

    let theKeys = [CNContactPostalAddressesKey] as [CNKeyDescriptor]

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

        let postalAddress = theName.postalAddresses
        postalAddress.forEach { (mailAddress) in

            // Strip forst 4 and last 4 from _$!<Home>!$_
            let aaa = mailAddress.label
            let bbb = aaa!.dropLast(4)
            let ccc = bbb.dropFirst(4)

            addressesArray.append(contactAddresses(theLabel: String(ccc), theID: mailAddress.identifier))
        }

        addressesArray.sort { $0.theLabel < $1.theLabel }

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

这是 tableView 扩展。当点击一个单元格时,addressID 将填充相应邮寄地址的 ID。

extension QuotePreview_VC: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return addressesArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let theCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

        theCell.textLabel?.text = addressesArray[indexPath.row].theLabel

        return theCell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
    {
        addressID = addressesArray[indexPath.row].theID
        populateThePrintFld()
        closeThePicker()
    }
}

【问题讨论】:

    标签: ios swift xcode


    【解决方案1】:

    这就是我最终要做的。

    if addressesArray.count > 1
            {
                // Grab the address picked by the user in the TV
                if let addressID = addressID
                {
                    if let finalPostalAddress = theName.postalAddresses.first(where: { labelValuePair in labelValuePair.identifier == addressID })?.value
                    {
                        mailAddress = CNPostalAddressFormatter.string(from: finalPostalAddress, style: .mailingAddress)
                    }
                }
            } else {
                // Grab the first available address
                if let firstPostalAddress = (theName.postalAddresses.first),
                    let labelValuePair = firstPostalAddress.value(forKey: "labelValuePair") as? NSObject,
                    let finalPostalAddress = labelValuePair.value(forKey: "value") as? CNPostalAddress
                {
                    mailAddress = CNPostalAddressFormatter.string(from: finalPostalAddress, style: .mailingAddress)
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2011-11-30
      • 1970-01-01
      相关资源
      最近更新 更多