通讯录

只是自己的简单使用,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];
}

你可能感兴趣的:(ios,框架,通讯录)