将facebook的联系人检索到我的应用程序并在表格视图中显示?

时间:2017-07-06 09:17:56

标签: ios objective-c facebook addressbook

我必须将facebook中的联系人显示到我的应用程序中。我没有得到如何执行此操作。 我正在使用此代码,但没有获得任何联系人列表。我在数组中得到这种类型,但没有获得任何facebook联系人列表。

<__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>,

请帮忙。

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

1 个答案:

答案 0 :(得分:0)

未运行以下代码,但请尝试根据需要进行一些更改。

-(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"]);

        }
      }
    }
}

希望这会有所帮助:) 编辑:经过测试和工作。