Question

When looping through all address book contacts is there a way to see whether a record is a Facebook-only record? That is address book records that are inserted when adding Facebook in iOS Settings.

I am trying to skip these records and only look at records that are user-entered or synced from other sources.

Thank you

No correct solution

OTHER TIPS

With this snipplet you can detect Facebook-Contacts:

+ (BOOL)isPersonFacebookContact:(ABRecordRef)person {
    ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);

    BOOL returnValue = NO;

    if (instantMessage) {
        for (NSInteger i=0 ; i < ABMultiValueGetCount(instantMessage); i++) {
            CFDictionaryRef instantMessageValue = ABMultiValueCopyValueAtIndex(instantMessage, i);
            CFStringRef instantMessageString = CFDictionaryGetValue(instantMessageValue, kABPersonInstantMessageServiceKey);

            if (CFStringCompare(instantMessageString, kABPersonInstantMessageServiceFacebook, 0) == kCFCompareEqualTo) {
                returnValue = YES;
            }

            CFRelease(instantMessageString);
            CFRelease(instantMessageValue);
        }
    }

    CFRelease(instantMessage);

    return returnValue;
}

Every ABPerson record belongs to exactly one ABSource.

When you iterate through all the person records, call ABPersonCopySource(person) on each. Facebook records should all be associated with a particular source record. I'm not sure how to identify it: possibly the kABSourceNameProperty for that source record will contain "Facebook".

This seems to work for me, Its doesn't detect facebook directly but you could evaluate the sources further and is there is a social profile linked that is facebook, and a linked contact to facebook contact then you would be pretty certain thats its facebook. I used this to get only the contacts I could write back to. Users can change their default contact creation account, but based on my test Facebook only contacts source never matches the ABAddressBookCopyDefaultSource.

CFArrayRef people = ABAddressBookCopyArrayOfAllPeopleInSource(ab , kABSourceTypeLocal);
AllContacts = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(people), people);


// Remove all the contacts that we cant write back to
for (int i = CFArrayGetCount(AllContacts)-1; i >-1  ; i--)
{
    ABRecordRef person = CFArrayGetValueAtIndex(AllContacts, i );
    if((ABPersonCopySource(person) !=ABAddressBookCopyDefaultSource(ab)))
    {
        NSString *fName = (NSString *)CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
        NSString *lName = (NSString *)CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));

        DLog(@"Removed a read only contact %@ %@", fName , lName);
        CFArrayRemoveValueAtIndex(AllContacts, i);
    }// else its not something you can write back to, like facebook, twitter.
    CFRelease(person);

}

New version (without memory licks):

+ (BOOL)isPersonFacebookContact:(ABRecordRef)person {
    ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);

    BOOL returnValue = NO;

    if (instantMessage) {
        for (NSInteger i = 0; i < ABMultiValueGetCount(instantMessage); i++) {
            CFDictionaryRef instantMessageValue = ABMultiValueCopyValueAtIndex(instantMessage, i);
            CFStringRef instantMessageString = CFDictionaryGetValue(instantMessageValue, kABPersonInstantMessageServiceKey);

            if (instantMessageString) {
                if (CFStringCompare(instantMessageString, kABPersonInstantMessageServiceFacebook, 0) == kCFCompareEqualTo) {
                    returnValue = YES;
                }
            }
            CFRelease(instantMessageValue);
        }

        CFRelease(instantMessage);
    }

    return returnValue; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top