Solomiya
Solomiya

Reputation: 320

Retrieve address from ABRecordRef

I need to get address information for calculating shipping and taxes rates for purchasing items on my app. I wanna use Apple Pay, thus I receive record as ABRecordRef instance. I've tried

NSString *zip = (__bridge NSString *)(ABRecordCopyValue(address, kABPersonAddressZIPKey));

but it causes EXC_BAD_ACCESS. I'm sure there should be a way to make it work, does anyone know it?

Upvotes: 1

Views: 922

Answers (2)

Solomiya
Solomiya

Reputation: 320

Figured it out finally:

CFTypeRef addressProperty = ABRecordCopyValue((ABRecordRef)address, kABPersonAddressProperty);
NSDictionary *addressDict = (__bridge NSDictionary *)CFArrayGetValueAtIndex((CFArrayRef)ABMultiValueCopyArrayOfAllValues(addressProperty), 0);

Resulting dictionary looks like this:

{
    City = Hillsborough;
    CountryCode = us;
    State = CA;
    ZIP = 94010;
}

Upvotes: 1

Tayyab
Tayyab

Reputation: 10631

Lets say you have record identifier of a contact and you want to retrieve address. Your code will be like following,

ABRecordRef address[1];
address[0] = ABAddressBookGetPersonWithRecordID(_addressBook, recId);
NSString *zip = (__bridge NSString *)(ABRecordCopyValue(address[0], kABPersonAddressZIPKey));

If you are doing something like this then please note in above code that you need to use address[0] instead of address in parameter to function ABRecordCopyValue.

I hope it helps.

Upvotes: 0

Related Questions