Aardvark
Aardvark

Reputation: 598

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

Hi can anyone tell me why ABMultiValueGetCount(social) always returns 0 count ? All other fields return from the address book perfectly.

I'm trying to see if contact has Twitter or Facebook active ? My users have Twitter and Facebook defined in the AB. I can see them there !!

/* The table view controller ViewDidLoad opened the AddressBook and copied these details into addressBookArray then I closed the reference to the AddressBook */
ABRecordRef person = (__bridge ABRecordRef)[addressBookArray objectAtIndex:indexPath.section];

/*  Make sure AddressBook is currently open so we can look at things */
ABAddressBookRef addressBook;
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

if (addressBook != NULL)
{
    NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

    /* If I don't do this person always returns NULL for social */
    ABRecordRef who = ABRecordCopyValue(ABAddressBookGetPersonWithRecordID (addressBook, ABRecordGetRecordID(person));

    /* ABMultiValueRef social = ABRecordCopyValue(person, kABPersonSocialProfileProperty); - always returns NULL */
    ABMultiValueRef social = ABRecordCopyValue(who, kABPersonSocialProfileProperty); // always returns a value

    /* C is always 0 even if user has FB/Twitter etc why ? */
    CFIndex c = ABMultiValueGetCount(social);

    CFRelease (addressBook);
}

I have read this question:

ABRecordCopyValue return 0 ?

However, if I edit an AB Contact and actually add twitter I still don't get a result. Is there another way of working this out ? Obviously Apple do it, as the Contacts App tells you about it.

Upvotes: 1

Views: 1821

Answers (1)

Aardvark
Aardvark

Reputation: 598

Alright, made some progress ....

Seems this property does work kABPersonInstantMessageProperty.

Sharing what I've done so far, as this seems to stump so many users. At least this code can be used as a basis to see if you have Skype, Yahoo, Facebook etc ..... but I want to get the FBID and that's held under kABPersonSocialProfileProperty. Why doesn't kABPersonSocialProfileProperty work as well as kABPersonInstantMessageProperty ? Is it an iOS 6 issue ....

*No it appears that unless you manually go to Settings - Facebook - Update All Contacts on your iOS device, the AddressBook has no knowledge for kABPersonSocialProfileProperty. Why is Apple inconsistent with the two properties ? In addition you have to select the linked contact that was created as the main contact also does not hold these details !!*

Also, why do I have to assign 'who' and can't use 'person' natively ?

Any one else got any more ideas ?

/*  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]);
        }

        if (socialApps != Nil)
            CFRelease(socialApps);

        howManySocialApps += thisSocialAppCount;
    }

    NSLog (@"Number of SocialApps Found is %i", howManySocialApps);

    CFRelease(addressBook);
}

Upvotes: 1

Related Questions