【问题标题】:How do I get just the street address from CNContacts in Xcode objective-c如何在 Xcode Objective-c 中从 CNContacts 获取街道地址
【发布时间】: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


    【解决方案1】:

    根据 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!");
        }
    }];
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      相关资源
      最近更新 更多