ios13 - 检查联系人 swift 5 的多个 postalAddresses
问题描述
我终于有时间回去做这件事了。下面的代码提取地址标签和 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)
}
}
解决方案
您可以使用以下代码获取多个 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)")
}
}
推荐阅读
- azure - 在 Azure 流分析 SAQL 中满足条件时的默认字段值
- mysql - 从mysql中的国家/地区获取最多投票时间/活动时间
- ios - 在 iOS 的 UIImageView 中使用 PNG 图像质量下降
- android - 如何在数据类参数没有默认值的情况下使用 dagger 在 android 中注入数据类?
- java - 继承类之间的静态方法 - Java
- pandas - 如何根据条件应用多个过滤条件以将其他列中的值复制到熊猫数据框中的新列中
- ios - 弹出视图控制器在 Xcode 11.2.1 中打开后立即关闭
- flutter - 几秒钟后,闪屏无法导航到重定向页面
- php - ClientException:客户端错误使用 laravel 在 GuzzleHttp 中给出“401 Unauthorized”错误
- c# - 如何使用依赖注入在 ASP.NET Core 中调用参数化构造函数?