【发布时间】:2021-05-16 15:06:13
【问题描述】:
我正在尝试获取 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。 有人可以告诉我如何做到这一点。
【问题讨论】:
标签: ios objective-c cncontactstore