在.h裏寫代理協議
,ABPeoplePickerNavigationControllerDelegate
在.m裏的viewload(哪裏需要調用寫在哪裏)寫這段代碼,
ABPeoplePickerNavigationController * picker = [[ABPeoplePickerNavigationController alloc] init];
[picker setPeoplePickerDelegate:self];
[self presentViewController:picker animated:YES completion:nil];
實現代理方法
#pragma ABPeoplePickerNavigationController delegate
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
return YES;
}
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier
{
if(property == kABPersonPhoneProperty) {
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, property);
int index = ABMultiValueGetIndexForIdentifier(phoneMulti,identifier);
NSString *phone = (NSString*)CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneMulti, index));
kABPersonFirstNameProperty));
NSString * str = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"+86" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@"(" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@")" withString:@""];
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
[_tfPhoneCode setText:str];
CFRelease(phoneMulti);//C對象的釋放
[peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
return NO;
}