通讯录(ios自带有界面)

1.添加AddressBookUI.framework框架

2控制器中实现

#import "ViewController.h"

#import <AddressBookUI/AddressBookUI.h>

 

@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    //1创建联系人选择控制器

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

    //2 设置自己的代理,不能设置成父类的代理

    peoPick.peoplePickerDelegate=self;

    //3 展示控制器

    [self presentViewController:peoPick animated:YES completion:nil];

}

//选择一个联系人就会调用

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

{

    //定一个CF语言字符串

    CFStringRef firststr=ABRecordCopyValue(person, kABPersonFirstNameProperty);

    CFStringRef laststr=ABRecordCopyValue(person, kABPersonLastNameProperty);

    //桥接的三种方式

    //1__bridge NSString * -->cf下的变量转换为oc下的对象,不转换权限

    //2__bridge_transfer NSString * -->cf下的变量转换为oc下的对象,做权限转换

    //2__bridge_retained CF-->cf下的变量转换为OC

    NSString*first=(__bridge_transfer  NSString *)(firststr);

    NSString*last=(__bridge NSString *)(laststr);

    NSLog(@"%@, %@",first,last);

    //first 不需要释放,last需要释放,如果对于空对象释放,程序直接崩溃

    if (laststr) {

        CFRelease(laststr);

    }

    //定义CF下的数组

    ABMultiValueRef multiValue=ABRecordCopyValue(person, kABPersonPhoneProperty);

    //取得 multivalue的个数

    CFIndex index=ABMultiValueGetCount(multiValue);

    for (CFIndex i=0; i<index; i++) {

        CFStringRef phone=ABMultiValueCopyValueAtIndex(multiValue, i);

        NSString *phonestr=(__bridge_transfer  NSString *)(phone);

        NSLog(@"%@",phonestr);

    }

    if (multiValue) {

        CFRelease(multiValue);

    }

}

 

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

//{

//    //property,对应某一个人某个的信息

//    NSLog(@"%zd",property);

//}

 

 

 

 

 

 

 

@end

原文地址:https://www.cnblogs.com/tangranyang/p/4656145.html