仅从地址簿中获取Facebook联系人

我只需从地址簿中获取Facebook联系人 . 我经历了很多线程,并提出了以下代码 .

- (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));
                }
            }
        }
    }
}

因为社交 Profiles 的计数是0,所以我无法获取任何联系人 . 在我从设置 - > Facebook同步我的Facebook帐户后,在地址簿中添加了许多联系人

请检查代码,如果有任何建议或我做错了,请告诉我 .

回答(1)

2 years ago

您正在尝试获取kABPersonSocialProfileUsernameKey,但它可以为空 . 尝试使用kABPersonSocialProfileUserIdentifierKey .