0

I have to show the contacts from facebook into my application.I am not getting how to do this. I am using this code but not getting any contact list. I am getting this type in the array but not getting any facebook list of contacts.

<__NSArrayM 0x608000457430>(
<CPRecord: 0x7fd37d5c3290 ABPerson>,
<CPRecord: 0x7fd37d5c34c0 ABPerson>,
<CPRecord: 0x7fd37d5c3d60 ABPerson>,
<CPRecord: 0x7fd37d5c3f90 ABPerson>,
<CPRecord: 0x7fd37d5c42e0 ABPerson>,
<CPRecord: 0x7fd37d5c4630 ABPerson>,
<CPRecord: 0x7fd37d5c4980 ABPerson>,
<CPRecord: 0x7fd37d5c4df0 ABPerson>,
<CPRecord: 0x7fd37d5c5140 ABPerson>,
<CPRecord: 0x7fd37d5c5490 ABPerson>,
<CPRecord: 0x7fd37d5c57e0 ABPerson>,
<CPRecord: 0x7fd37d5c5c50 ABPerson>,
<CPRecord: 0x7fd37d5c5fa0 ABPerson>,
<CPRecord: 0x7fd37d5c62f0 ABPerson>,
<CPRecord: 0x7fd37d5c6640 ABPerson>,
<CPRecord: 0x7fd37d404e10 ABPerson>,
<CPRecord: 0x7fd37d450e00 ABPerson>,
<CPRecord: 0x7fd37d460370 ABPerson>,
<CPRecord: 0x7fd37d460930 ABPerson>,
<CPRecord: 0x7fd37d44f730 ABPerson>,
<CPRecord: 0x7fd37d44f960 ABPerson>,
<CPRecord: 0x7fd37d460ff0 ABPerson>,
<CPRecord: 0x7fd37d461460 ABPerson>,
<CPRecord: 0x7fd37d4617b0 ABPerson>,
<CPRecord: 0x7fd37d461b00 ABPerson>,
<CPRecord: 0x7fd37d461fa0 ABPerson>,
<CPRecord: 0x7fd37d4624f0 ABPerson>,

Please help.

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, kABPersonSocialProfileUserIdentifierKey));
                }
            }
        }
    }
}
user7356913
  • 15
  • 3
  • 6

1 Answers1

0

Didn't run the below code, but try and make a few changes as required.

-(void) getFacebookContacts:(ABAddressBookRef) addressBookRef
{
    CFArrayRef allContacts = 
    ABAddressBookCopyArrayOfAllPeople(addressBookRef);
    CFIndex contactCount = CFArrayGetCount(allContacts);
    CFStringRef str1 = CFSTR("FACEBOOK");

    // 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) {
        for (NSInteger i=0 ; i < ABMultiValueGetCount(social); i++) {
            CFDictionaryRef instantMessageValue = ABMultiValueCopyValueAtIndex(social, i);
            CFStringRef instantMessageString = CFDictionaryGetValue(instantMessageValue, kABPersonInstantMessageServiceKey);
            // Check if service is FACEBOOK
            if (CFStringCompare(instantMessageString, str1, 0) == kCFCompareEqualTo) {
            returnValue = YES;

            NSDictionary * powerDic = (__bridge NSDictionary *)instantMessageValue;
            NSLog(@"%@", [powerDic valueForKey:@"service"]);
            NSLog(@"%@", [powerDic valueForKey:@"url"]);
            NSLog(@"%@", [powerDic valueForKey:@"username"]);
            NSLog(@"%@", [powerDic valueForKey:@"displayname"]);

        }
      }
    }
}

Hope this helps :) EDIT: Tested and working.

Tejas K
  • 682
  • 11
  • 28
  • its also not working , if(social) gets nil not going inside for (NSInteger i=0 ; i < ABMultiValueGetCount(social); i++) ,CFIndex count is 0 always – user7356913 Jul 06 '17 at 09:53
  • That's what we want, right ? If social is `nil`, then there is no need to execute the below code. – Tejas K Jul 06 '17 at 09:59
  • but CFArrayRef allContacts there are 42 elements – user7356913 Jul 06 '17 at 10:16
  • @user7356913: I have edited the code. I tried and it's working, check and let me know. – Tejas K Jul 06 '17 at 11:52
  • instantMessage what is this,undeclared identifier and returnValue @Tejas K – user7356913 Jul 06 '17 at 12:16
  • @user7356913: Check now. – Tejas K Jul 06 '17 at 12:18
  • when i am using breakpoint and moves cursor on social then this shows ABMultiValueRef 0x60000066ed00 with 0 value(s) , and not going inside for (NSInteger i=0 ; i < ABMultiValueGetCount(social); i++) this loop but CFIndex contactCount shows 42 – user7356913 Jul 06 '17 at 12:29
  • @user7356913: contactCount is the total number of contacts in your address book, it's showing zero because that contact has nothing under `social`. You should edit a contact and add values to `social` under `facebook` – Tejas K Jul 06 '17 at 12:32