I need to fetch only facebook contacts from address book. I have gone through many threads and have come up with following code.
- (IBAction)buttonPressed:(id)sender {
if (ABAddressBookRequestAccessWithCompletion) { // if in iOS 6
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
[self getFacebookContacts:addressBookRef];
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
[self getFacebookContacts:addressBookRef];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
}
else{ // if not in iOS 6
ABAddressBookRef addressBookRef = ABAddressBookCreate();
[self getFacebookContacts:addressBookRef];
NSLog(@"Not ios 6");
}
}
-(void) getFacebookContacts:(ABAddressBookRef) addressBookRef
{
CFArrayRef allContacts = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
CFIndex contactCount = CFArrayGetCount(allContacts);
// Traverse through contacts
for(int i=0; i<contactCount; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allContacts, i);
//Check if contact has social profile
ABMultiValueRef social = ABRecordCopyValue(( ABRecordRef)ref, kABPersonSocialProfileProperty);
if (social) {
// ***** This is coming as 0 **********
CFIndex count = ABMultiValueGetCount(social);
for (int i = 0 ; i < count; i++) {
CFDictionaryRef socialValue = ABMultiValueCopyValueAtIndex(social, i);
if(CFStringCompare( CFDictionaryGetValue(socialValue, kABPersonSocialProfileServiceKey), kABPersonSocialProfileServiceFacebook, 0)==kCFCompareEqualTo) {
NSLog(@"Facebook Contact %d : %@",i, (NSString*) CFDictionaryGetValue(socialValue, kABPersonSocialProfileUsernameKey));
}
}
}
}
}
Because the count that I am getting is 0 for social profile, I am not able to fetch any contacts. There are many contacts that have been added in address book after I sync my facebook account from settings->Facebook
Please check the code and let me know if there are any suggestions or if I am doing something wrong.