iOS通讯录的获取(addressBookUI)

1.创建选择联系人的界面

ABPeoplePickerNavigationController*ppad = [[ABPeoplePickerNavigationController alloc]init];

2.设置代理

ppad.peoplePickerDelegate=self;

3.弹出选择联系人页面

[selfpresentViewController:ppsd animated:YEScompletion:nil];

4.代理方法说明

4.1

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person

{

/*

__bridge NSString * :将CoreFoundation框架的对象所有权交给Foundation框架来使用,但是Foundation框架中的对象并不能管理该对象内存

__bridge_transfer NSString * :将CoreFoundation框架的对象所有权交给Foundation来管理,如果Foundation中对象销毁,那么我们之前的对象(CoreFoundation)会一起销毁

*/

// 1.获取选中联系人的姓名(姓lastname和名firstname)

CFStringRef firstname =ABRecordCopyValue(person,kABPersonFirstNameProperty);

CFStringRef lastname =ABRecordCopyValue(person,kABPersonLastNameProperty);

NSString *firstName = (__bridge_transferNSString*)(firstname);

NSString *lastName = (__bridge_transferNSString*)(lastname);

NSLog(@"%@ %@", firstName, lastName);

// 2.获取联系人的电话号码

ABMultiValueRef phones =ABRecordCopyValue(person,kABPersonPhoneProperty);

CFIndexcount =ABMultiValueGetCount(phones);

for(CFIndexi =0; i < count; i++) {

NSString *phoneLabel = (__bridge_transferNSString*)ABMultiValueCopyLabelAtIndex(phones, i);

NSString *phoneValue = (__bridge_transferNSString*)ABMultiValueCopyValueAtIndex(phones, i);

NSLog(@"label : %@ value : %@", phoneLabel, phoneValue);

}

// 3.释放不再使用的对象

CFRelease(phones);

}

选中一个联系人的时候会执行该代理方法,但是如果该方法实现了,则不会进入联系人的详细界面


iOS通讯录的获取(addressBookUI)_第1张图片

4.2

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier

进入联系人的详细界面后,选择该联系人的某个属性时,会执行该方法;

如果实现了该方法,那么选中联系人的属性时,就会返回主页面,不会进入下一个页面


iOS通讯录的获取(addressBookUI)_第2张图片

4.3

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController*)peoplePicker

点击了取消按钮会执行该方法

你可能感兴趣的:(iOS通讯录的获取(addressBookUI))