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

Posted

技术标签:

【中文标题】将 facebook 的联系人检索到我的应用程序中并在表格视图中显示?【英文标题】:Retrieve contacts of facebook into my application and show it in table view? 【发布时间】:2017-07-06 09:17:56 【问题描述】:

我必须在我的应用程序中显示来自 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】:

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

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

        
      
    

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

【讨论】:

它也不起作用,如果(社会)得到 nil 没有进入(NSInteger i=0 ; i 这就是我们想要的,对吧?如果social为nil,则无需执行以下代码。 但是 CFArrayRef allContacts 有 42 个元素 @user7356913:我已经编辑了代码。我试过了,它工作正常,检查并告诉我。 instantMessage 这是什么,未声明的标识符和返回值 @Tejas K

以上是关于将 facebook 的联系人检索到我的应用程序中并在表格视图中显示?的主要内容,如果未能解决你的问题,请参考以下文章

无法从 Facebook apprequest 检索数据

如何从测试用户那里检索 Facebook 照片?

如何从自定义帐户中检索联系方式?

如何从 MSN、Twitter、Facebook、GMail 等中检索联系人/电子邮件?

如何保护我的应用程序联系人,如 Facebook

如何将 Facebook 页面中的视频显示到我的 IOS 应用程序?