ABPeoplePickerNavigationController Facebook与本地数据

时间:2013-06-22 16:35:58

标签: ios objective-c facebook addressbook peoplepicker

我有一个完美的ABPeoplePickerNavigationController,但现在它没有抓住我的联系人的值,也没有代码更改。

该联系人有多个电话号码,过去人们选择器看到了所有这些,但现在它只是看到被标记为'facebook'的信息。所以它不再看到本地信息,只看到facebook关系中的信息。

最好是否有办法确保我只查看本地数据?

-OR -

有没有办法确定我正在查看哪种联系方式?我可以处理'facebook'数据与'本地',但我需要知道我正在使用哪种类型的记录来确定可用的数据。

这是我正在使用的方法(如果它是本地记录,这很有用):

- (void)pickPerson
{
    ABPeoplePickerNavigationController *peoplePickerController = [[ABPeoplePickerNavigationController alloc] init];
    peoplePickerController.peoplePickerDelegate = self;
    [self presentModalViewController:peoplePickerController animated:YES];  
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSMutableDictionary *contactInfo = [[NSMutableDictionary alloc] initWithCapacity:13];

    ////// Get Person's Full Name
    //
    NSString *name = (__bridge NSString *)ABRecordCopyCompositeName(person);
    if (name) {
        [contactInfo setObject:name forKey:@"name"];
    } else {
        [contactInfo setObject:@"" forKey:@"name"];
    }

    ////// Get Organization Name for Person
    //
    NSString *companyName = (__bridge NSString *)ABRecordCopyValue(person,kABPersonOrganizationProperty);
    if (companyName) {
        [contactInfo setObject:companyName forKey:@"companyName"];
    } else {
        [contactInfo setObject:@"" forKey:@"companyName"];
    }

    ////// Get phone
    //
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString *phoneLabel, *phoneStr;
    for (CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
        phoneLabel = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
        if ([phoneLabel isEqualToString:desiredPhoneLabel]) {
            phoneStr = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, i);
        }
    }
    if (phoneStr) {
        [contactInfo setObject:phoneStr forKey:@"phoneNumber"];
    } else {
        [contactInfo setObject:@"" forKey:@"phoneNumber"];
    }

    ////// Get twitter and facebook account for person
    //
    ABMutableMultiValueRef socialMulti = ABRecordCopyValue(person, kABPersonSocialProfileProperty);
    NSString *twitter, *facebook;
    for (CFIndex i = 0; i < ABMultiValueGetCount(socialMulti); i++) {
        NSDictionary *social = (__bridge NSDictionary *)ABMultiValueCopyValueAtIndex(socialMulti, i);
        if ([social[@"service"] isEqualToString:(__bridge NSString *)kABPersonSocialProfileServiceTwitter]) {
            twitter = (NSString *)social[@"username"];
        }
        if ([social[@"service"] isEqualToString:(__bridge NSString *)kABPersonSocialProfileServiceFacebook]) {
            facebook = (NSString *)social[@"username"];
        }
    }
    if (twitter) {
        [contactInfo setObject:twitter forKey:@"twitter"];
    } else {
        [contactInfo setObject:@"" forKey:@"twitter"];
    }
    if (facebook) {
        [contactInfo setObject:facebook forKey:@"facebook"];
    } else {
        [contactInfo setObject:@"" forKey:@"facebook"];
    }

    ////// Get address
    //
    ABMultiValueRef addresses = ABRecordCopyValue(person, kABPersonAddressProperty);
    NSString *addressLabel, *streetAddress, *cityName, *stateName, *zipCode, *countryName;
    if (ABMultiValueGetCount(addresses) > 0) {
        for (CFIndex i=0; i < ABMultiValueGetCount(addresses); i++) {
            addressLabel = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(addresses, i);
            if ([addressLabel isEqualToString:desiredAddressLabel]) {
                NSDictionary *addressDict = (__bridge NSDictionary *)ABMultiValueCopyValueAtIndex(addresses, i);
                streetAddress = [addressDict objectForKey:@"Street"];
                cityName = [addressDict objectForKey:@"City"];
                stateName = [addressDict objectForKey:@"State"];
                zipCode = [addressDict objectForKey:@"ZIP"];
                countryName = [addressDict objectForKey:@"Country"];
            }
        }
    }
    NSArray *streetAddressComponents = [streetAddress componentsSeparatedByString:@"\n"];
    if (streetAddressComponents[0]) {
        [contactInfo setObject:streetAddressComponents[0] forKey:@"addressStreet1"];
    } else {
        [contactInfo setObject:@"" forKey:@"addressStreet1"];
    }
    if (streetAddressComponents[1]) {
        [contactInfo setObject:streetAddressComponents[1] forKey:@"addressStreet2"];
    } else {
        [contactInfo setObject:@"" forKey:@"addressStreet2"];
    }
    if (cityName) {
        [contactInfo setObject:cityName forKey:@"city"];
    } else {
        [contactInfo setObject:@"" forKey:@"city"];
    }
    if (stateName) {
        [contactInfo setObject:stateName forKey:@"state"];
    } else {
        [contactInfo setObject:@"" forKey:@"state"];
    }
    if (zipCode) {
        [contactInfo setObject:zipCode forKey:@"postalCode"];
    } else {
        [contactInfo setObject:@"" forKey:@"postalCode"];
    }
    if (countryName) {
        [contactInfo setObject:countryName forKey:@"country"];
    } else {
        [contactInfo setObject:@"" forKey:@"country"];
    }

    ////// Get URL designated
    //
    ABMultiValueRef homepage = ABRecordCopyValue(person, kABPersonURLProperty);
    NSString *homepageLabel, *webPage;
    for (CFIndex i=0; i < ABMultiValueGetCount(homepage); i++) {
        homepageLabel = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(homepage, i);
        if ([homepageLabel isEqualToString:desiredHomepageLabel]) {
            webPage = (__bridge NSString *)ABMultiValueCopyValueAtIndex(homepage, i);
        }
    }
    if (webPage) {
        [contactInfo setObject:webPage forKey:@"webPage"];
    } else {
        [contactInfo setObject:@"" forKey:@"webPage"];
    }

    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
        [self dismissViewControllerAnimated:NO completion:nil];
    } else {
        [self dismissModalViewControllerAnimated:NO];
    }

    [self.delegate didUpdateContactInfo:self withInfo:contactInfo];
    CFRelease(addresses);

    return NO;
}

**更新:我很确定这与将iCloud添加到联系人有关,这使得本地联系人成为链接联系人'facebook'和'iCloud'的组合。所以现在我只需要检查链接的联系人并选择iCloud联系人。回到我的研究......

1 个答案:

答案 0 :(得分:0)

我在这里找到了解决方案:Getting merged/unified entries from ABAddressBook

简短版本:

使用

NSArray *allContactsForPerson = (__bridge NSArray *)(ABPersonCopyArrayOfAllLinkedPeople(person));

获取一个ABRecordRef数组,然后您可以在其中循环创建一个包含联系人所有链接信息的复合(或统一)联系人。

从复合材料中提取所需信息可以得到所需的结果。