大佬教程收集整理的这篇文章主要介绍了如何从 Xcode Objective-c 中的 CNContacts 获取街道地址,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试获取 CNContacts 中所有联系人的街道地址。我已经能够将 givenname 和 familyname 作为 NsString 获得,并且能够将 postalAddress 作为带有街道、城市、zip 等的数组获取,但我只想从数组中获取街道地址作为字符串.
这是我的代码
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOol granted,NSError * _Nullable error) {
if (granted == YES) {
//keys with fetching propertIEs
NSArray *keys = @[CNContactFamilynameKey,CNContactGivennameKey,CNContactPostalAddressesKey,CNPostalAddressstreetKey,CNPostalAddressCityKey,CNPostalAddresspostalCodeKey];
NsString *containerID = store.defaultContainerIDentifIEr;
nspreDicate *preDicate = [CNContact preDicateForContactsInContainerWithIDentifIEr:containerID];
NSError *error;
NSArray *cnContacts = [store unifIEdContactsmatchingPreDicate:preDicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@",error);
} else {
for (CNContact *contact in cnContacts) {
NsString *firstnames = contact.givenname;
NsString *lastnames = contact.familyname;
NSMutableArray *streetname = [[NSMutableArray alloc]initWithObjects:contact.postalAddresses,nil];
NSLog(@"streets:::%@",streetName); }}}}];
我使用的是 Objective-c,Swift 的例子很少,但 Objc 没有。 有人可以告诉我如何做到这一点。
根据 CNContact 对象 (https://developer.apple.com/documentation/contacts/cncontact/1403066-postaladdresses?language=objC) 的 postalAddresses 属性的文档,它是这样定义的:
NSArray<CNLabeledValue<CNPostalAddress*>*>* postalAddresses;
这意味着它包含一个 CNLabeledValue 对象数组,每个对象包含一个 CNPostalAddress。这允许将每个邮政地址与描述它的标签一起存储,并且还允许使用相同标签存储多个地址。
在上面的屏幕截图中,您可以看到,在向联系人添加地址时,允许用户从 4 个预定义标签中进行选择或创建自己的自定义标签(其中任何一个都可以多次使用)。
CNContactStore* store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts
completionHandler:^(BOOL granted,NSError * _Nullable error) {
if (error)
{
NSLog(@"Error accessing contacts %@",error.debugDescription);
return;
}
if (granted)
{
NSArray* keys = @[ CNContactFamilyNameKey,CNContactGivenNameKey,CNContactPostalAddressesKey,CNPostalAddressStreetKey,CNPostalAddressCityKey,CNPostalAddressPostalCodeKey
];
NSString* containerId = store.defaultContainerIdentifier;
NSPreDicate* preDicate = [CNContact preDicateForContactsInContainerWithIdentifier:containerId];
NSError* contactsError;
NSArray* contacts = [store unifiedContactsmatchingPreDicate:preDicate
keysToFetch:keys
error:&contactsError];
if (contactsError)
{
NSLog(@"Error fetching contacts %@",contactsError.debugDescription);
}
else
{
for (CNContact* contact in contacts)
{
NSString* firstName = contact.givenName;
NSString* lastName = contact.familyName;
NSLog(@"%@ %@:",firstName,lastName);
for ( CNLabeledValue* lVal in contact.postalAddresses )
{
// start with the assumption of a custom label
NSString* label = lVal.label;
if ( [CNLabelHome isEqualToString:label] )
{
label = @"Home";
}
else if ( [CNLabelWork isEqualToString:label] )
{
label = @"Work";
}
else if ( [CNLabelSchool isEqualToString:label] )
{
label = @"School";
}
else if ( [CNLabelOther isEqualToString:label] )
{
label = @"Other";
}
CNPostalAddress* address = (CNPostalAddress*)lVal.value;
NSLog(@"%@: [%@]",label,address.street);
}
}
}
}
else
{
NSLog(@"Contact access NOT granted!");
}
}];
上面的示例只是基于您发布的代码,并简单地记录了每个联系人的姓名,然后是为他们存储的每个(标记的)地址到控制台。在此之后,您可以做任何您想做的事情,例如将它们添加到您自己的数组中或进行任何您希望的进一步处理。
以上是大佬教程为你收集整理的如何从 Xcode Objective-c 中的 CNContacts 获取街道地址全部内容,希望文章能够帮你解决如何从 Xcode Objective-c 中的 CNContacts 获取街道地址所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。