1

我有一个运行良好的 ABPeoplePickerNavigationController,但现在它没有从我的联系人中获取值,也没有代码更改。

该联系人有多个电话号码,过去人员选择器可以看到所有电话号码,但现在它只看到标记为“facebook”的信息。所以它不再看到本地信息,只看到facebook关系中的信息。

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

-或者-

有没有办法确定我正在查看哪种类型的联系人?我可以处理“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 联系人。回到我的研究...

4

1 回答 1

0

我在这里找到了解决方案:从 ABAddressBook 获取合并/统一条目

简短版本:

利用

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

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

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

于 2013-06-22T19:26:52.060 回答