Failed to get Facebook profiles from ABAddressBook in iOS6
I am using the following code to extract social profiles from an address book in iOS 6:
// get the address book
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(options, error);
// get all people
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
// get the number of people
CFIndex noOfPeople = ABAddressBookGetPersonCount(addressBook);
// for all people
for (CFIndex personIndex = 0; personIndex<noOfPeople; personIndex++)
{
// get social profiles
NSMutableArray *socialProfilesArray = [[NSMutableArray alloc] init];
ABMultiValueRef socialProfiles = ABRecordCopyValue(person, kABPersonSocialProfileProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(socialProfiles); j++)
{
NSDictionary* socialProfile = (NSDictionary*)ABMultiValueCopyValueAtIndex(socialProfiles, j);
if ([socialProfile[@"service"] isEqualToString:(NSString*)kABPersonSocialProfileServiceFacebook])
{
NSString *strFacebook = (NSString*)socialProfile[@"username"];
}
else if ([socialProfile[@"service"] isEqualToString:( NSString*)kABPersonSocialProfileServiceTwitter])
{
NSString *twitterName = (NSString*)socialProfile[@"username"];
}
[socialProfilesArray addObject:socialProfile];
[socialProfile release];
}
CFRelease(socialProfiles);
}
It looks like as long as I get the Twitter profile successfully, the Facebook profile is not being returned. This is despite the fact that there is clearly a Facebook profile associated with one of my contacts, as indicated in the contacts app.
Any ideas why?
source to share
Hi I think I solved this question, I answered the question myself:
Perhaps this is because the user needs to manually update facebook with a button on iOS or the entries are not showing. I was stuck looking at what I saw in front of my eyes a facebook contact in the address book, but social ownership gave me nada !! Once I updated and also looked through the linked contacts, I found everything I needed. Also included is my code:
/* person is of type ABRecordRef loaded from an array thus:
addressBookArray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
.....
person = (__bridge ABRecordRef)[addressBookArray objectAtIndex:indexPath.section];
*/
ABAddressBookRef addressBook;
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
if (addressBook != Nil)
{
int howManySocialApps;
ABRecordRef who = ABAddressBookGetPersonWithRecordID (addressBook, ABRecordGetRecordID(person));
NSArray *linkedPeople = (__bridge_transfer NSArray *)ABPersonCopyArrayOfAllLinkedPeople (who);
for (int x = 0; x < [linkedPeople count]; x++)
{
ABMultiValueRef socialApps = ABRecordCopyValue((__bridge ABRecordRef)[linkedPeople objectAtIndex:x], kABPersonSocialProfileProperty);
CFIndex thisSocialAppCount = ABMultiValueGetCount(socialApps);
for (int i = 0; i < thisSocialAppCount; i++)
{
NSDictionary *socialItem = (__bridge_transfer NSDictionary*)ABMultiValueCopyValueAtIndex(socialApps, i);
NSLog(@"Social Item of type %@", [socialItem objectForKey:(NSString *)kABPersonSocialProfileServiceKey]);
/* put tests for facebook etc here */
}
if (socialApps != Nil)
CFRelease(socialApps);
howManySocialApps += thisSocialAppCount;
}
NSLog (@"Number of SocialApps Found is %i", howManySocialApps);
CFRelease(addressBook);
}
Another thing I found helpful was to make sure my array is managed, doesn't have a lot of duplicate contacts, i.e. login user for FB, Twitter, etc. Apple has some good code for this:
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
addressBookArray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);
Then, using linked contacts, we can find something.
Greetings.
source to share