AddressBook UI

Have you ever tried to get data fields from the address book and ran into the wonderful world of ABPerson reference only to find it reads like the blueprints to a space rocket?

Here is the straight out code on how to do it:

//Commented out code fails because the values are NOT STRINGS, but rather NSCFType)
//NSString *fone = (__bridge NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
//self.telLabel.text = fone;
//This on the other hand, does work
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFStringRef fone = ABMultiValueCopyValueAtIndex(phoneMulti, 0);
NSString *foneObject = (__bridge NSString*)fone;
NSLog(@”phone is %@”,foneObject);
self.telLabel.text = foneObject;

//Haven’t tried with URL
//NSString *url = (__bridge NSString*)ABRecordCopyValue(person, kABPersonURLProperty);
//self.urlLabel.text = [NSString stringWithFormat:url];

//SocialProfiles are a bit different
//This didn’t seem to work
//CFStringRef social = ABMultiValueCopyValueAtIndex(socialMulti, 0);
//NSString *socialObject = (__bridge NSString*)social;
//NSLog(@”social is %@”,socialObject);
//self.twitterName.text = socialObject;

//So i tried this but I can’t seem to get the if statement to work
ABMutableMultiValueRef socialMulti = ABRecordCopyValue(person, kABPersonSocialProfileProperty);
NSMutableDictionary *mySocialDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(socialMulti)];
NSLog(@”entering social dict of count %ld”, ABMultiValueGetCount(socialMulti));
for (CFIndex i = 0; i < ABMultiValueGetCount(socialMulti); i++) {
CFStringRef socialLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(socialMulti, i));
CFStringRef social = ABMultiValueCopyValueAtIndex(socialMulti, i);
if ([(__bridge NSString*)socialLabel isEqualToString:@"twitter"]) {
NSLog(@"we got a twitter");
[mySocialDict setObject:(__bridge NSString*)social forKey:(__bridge NSString*)socialLabel];
NSLog(@"social is %@",social);


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s