我需要给用户从地址簿中选择电话号码的机会,所以我从苹果手册中拿了个例子。但它只需要第一个号码,我如何使用户可以选择一个号码在地址簿中。
- (IBAction)adressBook:(UIButton *)sender {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
[self displayPerson:person];
[self dismissModalViewControllerAnimated:YES];
return NO;
}
- (void)displayPerson:(ABRecordRef)person {
NSString* phone = nil;
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,
kABPersonPhoneProperty);
if (ABMultiValueGetCount(phoneNumbers) > 0) {
phone = (__bridge_transfer NSString*)
ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
} else {
phone = @"[None]";
}
self.telNumber.text = phone;
CFRelease(phoneNumbers);
}发布于 2013-06-28 15:48:59
我用它来显示一个电话号码列表,这样我的用户就可以选择一个:
- (IBAction)getContact:(id)sender
{
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
picker.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
[self presentViewController:picker animated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
// ensure user picked a phone property
if(property == kABPersonPhoneProperty)
{
ABMultiValueRef phone = ABRecordCopyValue(person, property);
self.contactTextField.text = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, ABMultiValueGetIndexForIdentifier(phone, identifier));
[self dismissModalViewControllerAnimated:YES];
}
else
/* Display message if selection is not a phone number */
return NO;
}编辑:针对iOS 7和iOS 8更新的
// Delegate Method for iOS 7
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
// ensure user picked a phone property
if(property == kABPersonPhoneProperty)
{
ABMultiValueRef phone = ABRecordCopyValue(person, property);
self.contactTextField.text = (__bridge NSString *)ABMultiValueCopyValueAtIndex(phone, ABMultiValueGetIndexForIdentifier(phone, identifier));
[self dismissViewControllerAnimated:YES completion:nil];
}
else
/* Display message if selection is not a phone number */
return NO;
}
// Delegate Method for iOS 8
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
// Call the delegate method for iOS 7
[self peoplePickerNavigationController:peoplePicker shouldContinueAfterSelectingPerson:person property:property identifier:identifier];
}发布于 2013-06-28 15:46:25
这将返回Array,然后包含person拥有的所有数字。之后,您可以从数组中选择任何数字。
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
//get the phone number
ABMultiValueRef phone = (__bridge ABMultiValueRef)((__bridge NSMutableDictionary *)ABRecordCopyValue(person, kABPersonPhoneProperty));
NSArray *phoneArray = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phone);
NSMutableString *strPhone = [NSMutableString string];
for (int i=0; i<[phoneArray count]; i++)
{
[strPhone appendString:[NSString stringWithFormat:@"%@,",[phoneArray objectAtIndex:i]]];
}
NSLog(@"Dilip phoneArray : %@",phoneArray);
NSLog(@"Dilip strPhone : %@",strPhone);
phone = nil;
phoneArray = nil;
strPhone = nil;
[peoplePicker dismissModalViewControllerAnimated:YES];
return NO;
}发布于 2013-06-28 15:42:00
才能找到联系人;
- (IBAction)getContact:(id)sender{
ABPeoplePickerNavigationController *pickerPhone =
[[ABPeoplePickerNavigationController alloc] init];
pickerPhone.peoplePickerDelegate = self;
[self presentModalViewController:pickerPhone animated:YES];
[pickerPhone release];
}要返回到应用程序(关闭联系人视图):
- (void)peoplePickerNavigationControllerDidCancel: (ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}https://stackoverflow.com/questions/17359296
复制相似问题