1

I have a problem. I'm making an app in ios 6 that get contacts from my address book but if i have enabled the contacts option with facebook get all my contacts from my address book and from facebook and i would like if there is possible with code dont get facebook contacts. Only solutions that i thougth is or merge contacts with the same name or delete the contacts that have an email with @facebook.com.

Other solution?

2 Answers2

0

Checking for a @facebook.com email address is not reliable. Users can choose to not publish that and non-facebook address book entries could have that as their email address.

There is a special field in the address book called ExternalRepresentation that seems to carry some extra info about synced from facebook contacts. The first part of this always seems to be the same.

WARNING: This may not work all the time and probably will break someday in the future. This is undocumented.

static NSData *facebookExtRepPrefix = [NSData dataWithBytes:"bplist00\xd4\x01\x02\x03" length:12];
#define kABPersonExternalRepresentationProperty 39

then

NSData *extRep = (__bridge NSData *)ABRecordCopyValue(theRecord, kABPersonExternalRepresentationProperty);
BOOL isFacebook = [[extRep subdataWithRange:NSMakeRange(0, facebookExtRepPrefix.length)] isEqualToData:facebookExtRepPrefix];

you can then read the kABPersonPersonLinkProperty (#42) - that value will be the same on the native contact that is linked to it.

Hafthor
  • 16,358
  • 9
  • 56
  • 65
0

Using the default source i.e ABAddressBookCopyDefaultSource in ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering gave me the phone contacts (not Facebook contacts).

Rather than taking the count of people in the address book as ABAddressBookGetPersonCount, I used the allPeople array (from above) to get the number of people.

if (accessGranted) {

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);

    CFIndex nPeople = CFArrayGetCount(allPeople);//Not ABAddressBookGetPersonCount(addressBook) as that returns all contacts, Facebook included;
    items = [NSMutableArray arrayWithCapacity:nPeople];


    for (int i = 0; i < MIN(nPeople, 20); i++)
    {

        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

        //Do something with this person
    }
}

Ref: Get all contacts: https://stackoverflow.com/a/17976915/1349663

Community
  • 1
  • 1
user1349663
  • 595
  • 1
  • 7
  • 21