I am retrieving contact's multiple addresses but only want Home address

418 views Asked by At

Does anyone know how to retrieve the Home address from multiaddress in iOS? I have got permission from user and everything else but the problem is, I only want the Home address.

ABAddressBookRef addressBook = ABAddressBookCreate();
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    dispatch_release(sema);
}
else { // we're on iOS 5 or older

    accessGranted = YES;
}

if (accessGranted) {

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

    NSMutableArray *array = [[NSMutableArray alloc] init];

    for( int i = 0 ; i < nPeople ; i++ )

    {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i );

        // First Name
        NSString *fName = (__bridge NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);

        // Last Name
        NSString *lName = (__bridge NSString*)ABRecordCopyValue(ref, kABPersonLastNameProperty);

        // Phone
        ABMultiValueRef phoneMultiValue = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFArrayRef allPhones = ABMultiValueCopyArrayOfAllValues(phoneMultiValue);
        NSMutableArray *phoneData = [NSMutableArray arrayWithArray:(__bridge NSArray*) allPhones];

        // Email
        ABMultiValueRef emailMultiValue = ABRecordCopyValue(ref, kABPersonEmailProperty);
        CFArrayRef allEmails = ABMultiValueCopyArrayOfAllValues(emailMultiValue);
        NSMutableArray *emailData = [NSMutableArray arrayWithArray:(__bridge NSArray*) allEmails];

        // Address
        ABMultiValueRef addressMultiValue = ABRecordCopyValue(ref, kABPersonAddressProperty);

        CFArrayRef allAddresses = ABMultiValueCopyArrayOfAllValues(addressMultiValue);

        NSMutableArray* addressData = [NSMutableArray arrayWithArray:(__bridge NSArray*) allAddresses];

        for ( int j = 0 ; j < [addressData count]; j++) {
            if ([[addressData objectAtIndex:j] count] > 0) {

                if ([fName length] > 0 || [lName length] > 0) {


                    if ([fName length] > 0) {
                        [dic setObject:fName forKey:@"FirstName"];
                    }

                    if ([lName length] > 0) {
                        [dic setObject:lName forKey:@"LastName"];
                    }

                    if ([phoneData count] > 0) {
                        [dic setObject:phoneData forKey:@"MultiplePhoneNumbers"];
                    }

                    if ([emailData count] > 0) {
                        [dic setObject:emailData forKey:@"MultipleEmails"];
                    }


                    [dic setObject:addressData forKey:@"MultipleAddresses"];



                }
            }
        }

        NSUInteger keyCount = [[dic allKeys] count];
        if (keyCount > 0) {

            ABRecordID recId = ABRecordGetRecordID(ref);


            [dic setObject:[NSString stringWithFormat:@"%d",recId] forKey:@"ABRecordRef"];
            [dic setObject:[NSString stringWithFormat:@"%d",i] forKey:@"ab_id"];
            [dic setObject:[NSNumber numberWithBool:FALSE] forKey:@"is_coordinate_fetch"];
            [array addObject:dic];                
        }

I would really appreciate if anyone could take time and solve this for me.

2

There are 2 answers

2
David H On

There is no single solution to this problem. What I did in my app to do this was as follows:

1) If the person just had one address, use that.

2) If there was more than one address, using cells that had the name and the first address, and a button that said "Select different Address". If the user taps that button, animate up a sheet where the user sees another table of all addresses, and can choose the one they want.

0
Marco On

You need to iterate through the kABPersonAddressProperty multivalue property and extract the one matching the kABHomeLabel identifier. Here is how you could do it in iOS 7 (assumes a reference to the address book):

NSArray *people = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(localAddressBook));

// Iterate through each person in the Address Book
for (NSUInteger i = 0; i < people.count; i++)
{
    ABRecordRef person = CFArrayGetValueAtIndex((__bridge CFArrayRef)people, i);

    // Access the person's addresses (a ABMultiValueRef)
    ABMultiValueRef addressesProperty = CFAutorelease(ABRecordCopyValue(person, kABPersonAddressProperty));

    if (addressesProperty)
    {
        // Iterate through the address multivalue
        for (CFIndex index = 0; index < ABMultiValueGetCount(addressesProperty); index++)
        {
            // Get the address label
            NSString *addressLabel = (NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(addressesProperty, index));

            // Check for home address label
            if ([addressLabel isEqualToString:(NSString *)kABHomeLabel])
            {
                // Your code here
                NSLog(@"%@", addressLabel);
            }
        }
    }
}