如何使用邮寄地址标签拉取联系人的邮寄地址?
下面的函数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()
}
}发布于 2020-04-25 04:47:28
这就是我最终要做的事情。
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)
}
}https://stackoverflow.com/questions/61035781
复制相似问题