//
// ViewController.m
// MobileAddress
//
// Created by hq on 16/5/16.
// Copyrigaht 2016年 hanqing. All rights reserved.
//
#import "ViewController.h"
#import <AddressBookUI/AddressBookUI.h>
@interface ViewController () <ABPeoplePickerNavigationControllerDelegate>
- (IBAction)pop:(UIButton *)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)pop:(UIButton *)sender {
ABPeoplePickerNavigationController *picNav=[[ABPeoplePickerNavigationController alloc]init];
picNav.peoplePickerDelegate=self;
[self presentViewController:picNav animated:YES completion:nil];
}
#pragma mark 通过代理监听选择情况
/**
选择了某个联系人
只要实现了该方法之后,点击某个联系人后控制器会自动消失
*/
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person{
//获取联系人的姓名
CFStringRef firstName=ABRecordCopyValue(person, kABPersonFirstNameProperty);
CFStringRef lastName=ABRecordCopyValue(person, kABPersonLastNameProperty);
/**
(__bridge NSString *): 将CoreFoundation框架的对象所有权交给Foundation框架来使用,但是Foundation不能管理其内存,所以最后我们需要手动释放
(__bridge_transfer NSString *): CoreFoundation框架的对象所有权交给Foundation管理,如果Foundation中对象销毁,那么我们之前的CoreFoundation中对象一起销毁
*/
NSString *firstNameStr=(__bridge_transfer NSString *)(firstName);
NSString *lastNameStr=(__bridge_transfer NSString *)(lastName);
NSLog(@"%@%@",firstNameStr,lastNameStr);
//获取手机号-数组
ABMultiValueRef phones= ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex count=ABMultiValueGetCount(phones);
for (int i=0; i<count; i++) {
NSString *phoneLabel=(__bridge_transfer NSString * )ABMultiValueCopyLabelAtIndex(phones, i);
NSString *phoneValue=(__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
NSLog(@"%@:%@",phoneLabel,phoneValue);
}
CFRelease(phones);
}
/**
选择了某个人的属性
点击属性后,界面将消失
*/
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
NSLog(@"选择了某个属性");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
AddressBookUI获取通讯录
最新推荐文章于 2022-05-02 23:26:52 发布