Reputation: 7822
Overview: I am trying to get the phone number and full name when people use the peoplePicker and clicks on the name. Then, I'd like to display the full name on a textfield and save the phone number as a string. Using the ph num and name, I intend to use that as a unique identification. I don't want to use the unique id of ABRecord because sometimes i have duplicates on my contacts especially when I sync it with google, etc...
If I understand correctly, I need to use this delegate method
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
Using the above, I can get the full name to display on the text field as scuh
textField.text = ABRecordCopyCompositeName(person);
But, I don't know how to get the ph number. For me to get the ph number, I have to use the other delegate method:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,identifier);
However, I don't like this because then when the user clicks on the name on the address book, it shows the detail with the ph number, email, etc and the user has to click on the ph number. What I want is from the first screen, the user clicks on the name and the name gets displayed as a textField and the ph number is saved as a string somewhere.
Upvotes: 1
Views: 1966
Reputation: 27598
If you want to get just the first email and phone number use this code. This is for iOS 5.0 with Xcode 4.2
- (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;
}
Upvotes: 1
Reputation:
I use a method to save all persons with emails in a array and then display the array in a table view:
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];
Upvotes: 1