通讯录

只是自己的简单使用,ios9又出现了新的框架,完全是oc语言版的,更加方便易懂。
直接转载了别人的一篇,感觉很不错。
http://blog.csdn.net/u013196181/article/details/51065905

1. 导入头文件
import <AddressBookUI/AddressBookUI.h>
// 在ios9.0已经弃用
//<AddressBookUI/AddressBookUI.h> 中的ABPeoplePickerNavigationController 被 ContactsUI.framework中的CNContactPickerViewController取代
2. 简单实现
  // 1.创建ABPeoplePickerNavigationController控制器
    ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];

    // 2.设置代理
    ppnc.peoplePickerDelegate = self;

    // 3.弹出控制器
    [self presentViewController:ppnc animated:YES completion:nil];
3. ABPeoplePickerNavigationController的代理方法

如果使用下面的代理方法会拦截页面跳转

// =================================iOS8=================================
/**
 *  选中某一个联系人的时候会调用该方法
 *
 *  @param person       选中的联系人
 */
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    // 1.取出联系人的姓名
    NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
    NSLog(@"firstName:%@ lastName:%@", firstName, lastName);
    // 2.取出联系人的电话
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFIndex phoneCount = ABMultiValueGetCount(phones);
    for (int i = 0; i < phoneCount; i++) {
        NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
        NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"%@--%@", phoneLabel, phoneValue);
    }

    // 3.释放不需要的对象
    CFRelease(phones);
}
/**
 *  选中某一个联系人的某一个属性的时候会调用该方法
 *
 *  @param person       选中的联系人
 *  @param property     选中的属性
 *  @param identifier   属性对应的标识符
 */
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{

}

ios8不执行下面的方法

// =================================iOS7=================================
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    // 退出控制器
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
    // 如果返回\(^o^)/YES!则不执行下面的方法,且把上面一行注释掉
    return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    return YES;
}

/**
*   必须实现该方法,否则点击取消按钮会崩
 *  当点击取消按钮的时候会调用该方法
 */
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [peoplePicker dismissViewControllerAnimated:YES completion:nil];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值