iOS开发-通讯录有界面

//

//  ViewController.m

//  06-通讯录(有界面)

//

 

#import "ViewController.h"

#import <AddressBookUI/AddressBookUI.h>

@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate>

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

}

 

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

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

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

    peoplePicker.peoplePickerDelegate = self;

    [self presentViewController:peoplePicker animated:YES completion:^{

        

    }];

    

}

 

//用户选择一个联系人可以挑用这个方法(如果实现了这个方法,就不会调用下面这个方法)

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

//    ABRecordRef

//    record -> 记录

    //底层有一个数据库,一个记录对应一个联系人的信息

    //name  id(学号)  age

    

//    NSLog(@"%d",person);

//    <#ABPropertyID property#>告诉我这条记录里面的什么内容

   CFStringRef firstStr =  ABRecordCopyValue(person,kABPersonFirstNameProperty);

    CFStringRef lastStr =  ABRecordCopyValue(person,kABPersonLastNameProperty);

    //桥接

    NSString *first = (__bridge NSString *)(firstStr);

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

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

    CFRelease(firstStr);

    CFRelease(lastStr);

    //电话

    //AB框架里面的东西  等价于NSArray

    //住宅,公司 手机

    //CF框架下,内存自己管理,如果对象是  copy create等需要手动realse

  ABMultiValueRef  multiValueRef =  ABRecordCopyValue(person, kABPersonPhoneProperty);

   CFIndex index =  ABMultiValueGetCount(multiValueRef);

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

        //根据索引获取需要的电话

       CFStringRef phoneStr = ABMultiValueCopyLabelAtIndex(multiValueRef, i);

        NSString *phone = (__bridge NSString *)(phoneStr);

        NSLog(@"%@",phone);

        CFRelease(phoneStr);

        

    }

    CFRelease(multiValueRef);

    

    

}

 

//一个属性选择之后就会调用

//标识

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

    //property 属性 对应一个人的电话,地址,生日

    //如果有很多个的话,identifier来区分

    NSLog(@"%d",property);

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

posted @ 2017-07-03 14:43  李洪强  阅读(412)  评论(0编辑  收藏  举报