如何从 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。这允许将每个邮政地址与描述它的标签一起存储,并且还允许使用相同标签存储多个地址。

enter image description here

enter image description here

在上面的屏幕截图中,您可以看到,在向联系人添加地址时,允许用户从 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!");
    }
}];

上面的示例只是基于您发布的代码,并简单地记录了每个联系人的姓名,然后是为他们存储的每个(标记的)地址到控制台。在此之后,您可以做任何您想做的事情,例如将它们添加到您自己的数组中或进行任何您希望的进一步处理。