1

概述:当人们使用 peoplePicker 并单击名称时,我正在尝试获取电话号码和全名。然后,我想在文本字段上显示全名并将电话号码保存为字符串。使用 ph num 和 name,我打算将其用作唯一标识。我不想使用 ABRecord 的唯一 ID,因为有时我的联系人中有重复项,尤其是当我将其与 google 等同步时...

如果我理解正确,我需要使用这个委托方法

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
      shouldContinueAfterSelectingPerson:(ABRecordRef)person 

使用上述方法,我可以将全名显示在文本字段上作为 scuh

textField.text = ABRecordCopyCompositeName(person);

但是,我不知道如何获得 ph 值。为了让我获得 ph 号码,我必须使用其他委托方法:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier

    ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
    NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,identifier);

但是,我不喜欢这样,因为当用户单击通讯簿上的姓名时,它会显示带有 ph 号、电子邮件等的详细信息,并且用户必须单击 ph 号。我想要的是从第一个屏幕上,用户单击名称,名称显示为文本字段,并且 ph 号在某处保存为字符串。

4

2 回答 2

1

我使用一种方法将所有人的电子邮件保存在一个数组中,然后在表格视图中显示该数组:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);


NSMutableArray *mArr = [[NSMutableArray alloc]init];

for( int i = 0 ; i < nPeople ; i++ )
{
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
    NSString *preName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *postName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
    int ecount = ABMultiValueGetCount(emails);

    for (int i = 0; i < ecount; i++) {
        NSMutableDictionary *dd = [[NSMutableDictionary alloc]init];

        [dd setValue:[NSString stringWithFormat:@"%@ %@", preName, postName] forKey:@"name"];

        NSString *emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails, i);
        [dd setValue:emailID forKey:@"mail"];
        //NSLog(@"inside loop %@ %@ %@", preName, postName, emailID);
        [emailID release];


        [mArr addObject:dd];
        [dd release];
    }
}

emailArray = [[NSArray alloc]initWithArray:mArr];
[mArr release];
于 2010-12-18T17:18:55.760 回答
1

如果您只想获取第一个电子邮件和电话号码,请使用此代码。这适用于带有 Xcode 4.2 的 iOS 5.0

- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person 
{

    //NSLog(@"Went here 1 ...");

    NSString *nameStr = (__bridge NSString *)ABRecordCopyCompositeName(person);
    ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
    ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);

    NSString *emailStr = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emails, 0);
    NSString *phoneStr = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phones, 0);

    //strip number from brakets
    NSMutableString *tmpStr1 = [NSMutableString stringWithFormat:@"%@", phoneStr];
    NSString *strippedStr1 = [tmpStr1 stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@"()-"];
    strippedStr1 = [[strippedStr1 componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""];

    NSLog(@"nameStr: %@ ... emailStr: %@ ... phoneStr: %@ ...", nameStr, emailStr,strippedStr1);

    //dismiss
    [self dismissModalViewControllerAnimated:YES];

    return NO;
}
于 2013-01-20T21:44:31.827 回答