2

I am replacing my obsolete ABAdressBook code with the current CNContact framework in Objective C. I could sort out most of it, except for the Home City part, so lets focus on that. Currently I have this code:

    -(NSArray *)getLandAddressesForContactIOS6:(ABRecordRef)recordRef {

        ABMultiValueRef addresses = ABRecordCopyValue(recordRef, kABPersonAddressProperty);
        NSMutableArray *formattedAddressesResponse = [NSMutableArray array];

        for(CFIndex i = 0; i < ABMultiValueGetCount(addresses); i++) {

            NSString *label = (__bridge NSString *)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(addresses, i));
            NSDictionary *addressComponents = (__bridge NSDictionary*)ABMultiValueCopyValueAtIndex(addresses, i);

            NSString *street = [addressComponents objectForKey:(NSString *)kABPersonAddressStreetKey];
            NSString *city = [addressComponents objectForKey:(NSString *)kABPersonAddressCityKey];            
            NSString *formattedAddress = ABCreateStringWithAddressDictionary(addressComponents, YES);

            NSMutableDictionary *currentAddressResponse = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                           label, @"type",
                                                           label, @"label",
                                                           nil];
            if (street != nil) {
                [currentAddressResponse setObject:street forKey:@"street"];
            }
            if (city != nil) {
                [currentAddressResponse setObject:city forKey:@"city"];
            }
            if (formattedAddress != nil) {
                [currentAddressResponse setObject:formattedAddress forKey:@"formattedAddress"];
            }

            [formattedAddressesResponse addObject:currentAddressResponse];
        }

        return formattedAddressesResponse;
    }

That code is deprecated for iOS 9+ so the closest I have got to get the home city with the new Contacts framework is:

-(NSArray *)getLandAddressesForContactIOS10:(CNContact*)recordRef {
    NSArray <CNLabeledValue<CNPostalAddress *> *> *addresses = recordRef.postalAddresses;

    NSMutableArray *formattedAdressResponse = [NSMutableArray array];
    for(CFIndex i = 0; i < addresses.count; i++) {
        CNLabeledValue *addressi = [addresses objectAtIndex:i];
        //NSString *city = addressi.??????; //Stuck here, don't know what else to do

How can I extract the City name from a CNContact??

Josh
  • 6,251
  • 2
  • 46
  • 73

2 Answers2

1
-(void )getLandAddressesForContactIOS10:(CNContact*)contact
{
    for (CNLabeledValue<CNPostalAddress*>* labeledValue in contact.postalAddresses)
    {

            NSLog(@"%@",labeledValue.value.city);

            NSLog(@"%@",labeledValue.value.street);

            NSLog(@"%@",labeledValue.value.state);

            NSLog(@"%@",labeledValue.value.postalCode);

            NSLog(@"%@",labeledValue.value.ISOCountryCode);

    }
}
Deepesh
  • 8,065
  • 3
  • 28
  • 45
1
NSArray *addresses = (NSArray*)[contact.postalAddresses valueForKey:@"value"];

        if (!(addresses == nil) && addresses.count > 0)
        {
            for (CNLabeledValue<CNPostalAddress*>* labeledValue in contact.postalAddresses)
            {      
                NSString *city = labeledValue.value.city;
                NSLog(@"City = %@",city);

                NSString *street = labeledValue.value.street;
                NSLog(@"Street = %@",street);

                NSString *state = labeledValue.value.state;
                NSLog(@"State = %@",state);

                NSString *postalCode = labeledValue.value.postalCode;
                NSLog(@"PostalCode = %@",postalCode);

                NSString *ISOCountryCode = labeledValue.value.ISOCountryCode;
                NSLog(@"ISOCountryCode = %@",ISOCountryCode);   
            }
        }
        else
        {
            NSLog(@"No addresses for name = %@",strname);
        }