ntcio
ntcio

Reputation: 117

Unable to get Facebook profiles from ABAddressBook in iOS6

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?

Upvotes: 3

Views: 1274

Answers (1)

Aardvark
Aardvark

Reputation: 598

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.

Upvotes: 1

Related Questions