我只需要从通讯录中获取 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));
}
}
}
}
}
因为我得到的社交资料计数为 0,所以我无法获取任何联系人。从设置->Facebook同步我的Facebook帐户后,地址簿中添加了许多联系人
请检查代码,如果有任何建议或我做错了什么,请告诉我。