3

I am using the following to code to retrieve the social profiles from 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 appears that while I successfully get a Twitter profile, no Facebook profile is returned. And that is despite that there is clearly a Facebook profile associated with one of my contacts, as indicated in the contacts application.

Any ideas why?

ntcio
  • 117
  • 9
  • I know you say "clearly associated", but I'm going to ask for clarification anyway. You mean the "Facebook" field exists and is populated with a username and NOT the Website field is populated with the facebook URL scheme right? – Fruity Geek Jan 24 '13 at 18:22
  • Hello. What I am trying to say is that when I go the Apple Contacts app, I can see a person with the little FB badge at the bottom left side of the picture, and the fields: email, birthday, Facebook, IM are all populated with details from a FB account. But, when I run the code above I get no Facebook account returned. ABMultiValueGetCount(socialProfiles) would return 0. – ntcio Jan 25 '13 at 09:11
  • 1
    You should look at his linked contacts too. See [here for more info][1] [1]: http://stackoverflow.com/questions/13032565/ios-6-address-book-empty-kabpersonphoneproperty – Idan Feb 25 '13 at 12:47
  • "This method will never work until the user marge the FaceBook Contacts with native contacts." See here: http://stackoverflow.com/a/14733022/645314 – OpenUserX03 Jun 12 '14 at 03:24

1 Answers1

1

Hi I think I solved this go to me self answered question:

ABMultiValueGetCount - kABPersonSocialProfileProperty always returns 0 ? How can I get Facebook details from address book?

It may be due to the fact the user has to manually update facebook via a button on iOS or the records don't appear. I was stuck looking at the fact before my eyes I could see the facebook contact in address book, but the social property gave me nada !! Once I updated and also scanned through linked contacts I found everything I needed. Also included is my code to do so:

/*  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 useful was to make sure my array being manipulated didn't have lots of duplicate contacts i.e. entry user for FB, Twitter etc. Apple have a nice bit of code to do this:

    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    addressBookArray = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);

Then using linked contacts we can find anything.

Cheers.

Community
  • 1
  • 1
Aardvark
  • 608
  • 5
  • 15