How do you select a certain number from a phone's address book?
How do you select a certain number from a phone's address book?
I get a contact from the Address book, but only mobile number is retrieved. What should I do to enable user to select mobile/home/other number?
Here is my code:
-(IBAction)pickContact
{
// creating the picker
ABPeoplePickerNavigationController *picker = [[ABPeople开发者_如何学PythonPickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
// showing the picker
[self presentModalViewController:picker animated:YES];
// releasing
[picker release];
}
-(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];
}
-(BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
Contact *cont=[[Contact alloc] init];
// setting the first name
cont.fName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
// setting the last name
cont.lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
// setting the number
/*
this function will set the first number it finds
if you do not set a number for a contact it will probably
crash
*/
//ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
// cont.number = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(
person, kABPersonPhoneProperty);
CFIndex numPhoneNums = ABMultiValueGetCount(phones);
if(numPhoneNums == 0) {
NSLog(@"No number available");
cont.number = @"No number available";
} else {
cont.number = (NSString*) ABMultiValueCopyValueAtIndex(phones, 0);
}
...The rest works alright.
To get all type of phone numbers...(person is an instance of ABRecordRef)
NSMutableArray *arPhList = [[NSMutableArray alloc] init];
ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
{
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phones, j));
NSString *phoneNumber = (NSString *)phoneNumberRef;
NSDictionary *dicTemp = [[NSDictionary alloc]initWithObjectsAndKeys:phoneNumber,@"value", phoneLabel,@"label", nil];
[arPhList addObject:dicTemp];
}
else {
cont.number = (NSString*) ABMultiValueCopyValueAtIndex(phones, **0**);
}
"phones" has all phone numbers (mobile, home, fax, etc)
But you only use index 0.
You use only first phone number. (It would be not mobile.)
Use for loop (numPhoneNums) to access all numbers.
精彩评论