iPhone OS SDK – Contact Address Property

17/03/2010

Today something quick. I’m working with addressbook information and I am extracting data from a contact, I already know I want in the addressbook (addressbookID=45 lets say).
How do I get their address?
And their Name, Company, Phone Number and Email!


Use the following.

NSLog(@”Looking Up Contact Now”);
ABAddressBookRef ab = ABAddressBookCreate();
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,item2.integerValue);

NSLog(@”person: %d”,person);
if (person != nil) {

//Name
myName = (NSString *)ABRecordCopyCompositeName(person);

//Company
myCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);

//Email Address (this one is tricky)
ABMutableMultiValueRef emailMulti = ABRecordCopyValue(person, kABPersonEmailProperty);
NSMutableArray *emails = [[NSMutableArray alloc] init];
for (int i = 0; i < ABMultiValueGetCount(emailMulti); i++) {
NSString *anEmail = [(NSString*)ABMultiValueCopyValueAtIndex(emailMulti, i) autorelease];
[emails
addObject:anEmail];
}


if([emails count] > 0) myEmail = [emails objectAtIndex:0];

//Phone Numbers
ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNumbers = (NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNumberProperty);

// Do whatever you want with the phone numbers
NSLog(@”Phone numbers = %@”, phoneNumbers);

//Location Address
ABMutableMultiValueRef addressMulti = ABRecordCopyValue(person, kABPersonAddressProperty);
NSMutableArray *address = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < ABMultiValueGetCount(addressMulti); i++) {
NSString *city = [(NSString*)ABMultiValueCopyValueAtIndex(addressMulti, i) autorelease];
[address
addObject:city];
}
NSLog(@”addresses: %@”,address);

} else {
UIAlertView *baseAlert = [[UIAlertView alloc] initWithTitle:@”Address Link” message:@”Sorry, no address book link available” delegate:self cancelButtonTitle:nil otherButtonTitles:@”OK”, nil];
[baseAlert
show];
}

No comments yet.

Write a comment:

blog comments powered by Disqus