首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用地址标签swift 5获取联系人的邮件地址

使用地址标签swift 5获取联系人的邮件地址
EN

Stack Overflow用户
提问于 2020-04-05 06:48:59
回答 1查看 165关注 0票数 0

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

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

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

代码语言:javascript
复制
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)

}

代码语言:javascript
复制
struct contactAddresses
{
    var theLabel: String
    var theID: String
}

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

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

代码语言:javascript
复制
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。

代码语言:javascript
复制
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。

代码语言:javascript
复制
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()
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-04-25 04:47:28

这就是我最终要做的事情。

代码语言:javascript
复制
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)
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61035781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档