iOS - 获取通讯录

好久没写博客了,婚结完了,也该开始写一点东西了,先从OC的整理开始。

通讯录一定要记得在 info.plist 文件里写上权限请求,不然后果很严重呦。

另外请记得加入“AddressBook.framework”,并且引用 #import


一、代码

#pragma mark 获取通讯录权限
- (void)accessPermissionsForBook{
    //获取通讯录权限状态
    ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();
    if (authorizationStatus == kABAuthorizationStatusAuthorized) {
        //如果权限是用户已经同意,那么直接调取通讯录
        [self haveAddressBook];
    }else{
        // 如果是其他权限,请求授权
        ABAddressBookRef addressBookRef =  ABAddressBookCreate();
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                // 授权成功,调取通讯录
                [self haveAddressBook];
            } else {
                // 授权失败,做出相应的操作,如提示“@"请到 设置>隐私>通讯录打开本应用的权限设置"”
                NSLog(@"授权失败!");
            }
        });
    }
}

#pragma mark 获取通讯录内容
- (void)haveAddressBook{
    // 2. 获取所有联系人
    ABAddressBookRef addressBookRef = ABAddressBookCreate();
    CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
    long count = CFArrayGetCount(arrayRef);
    for (int i = 0; i < count; i++) {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
        //获取联系人对象的引用
        ABRecordRef people = CFArrayGetValueAtIndex(arrayRef, i);
        //获取当前联系人名字
        NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
        /*
         如果没有获取到联系人姓名,可以试试下面的两个方法,含义见文章最后。
         firstName = (__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
         firstName = (__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
         */
        
        //获取当前联系人的电话 数组
        NSMutableArray *phoneArray = [[NSMutableArray alloc]init];
        ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);
        for (NSInteger j=0; j


二、info.plist文件

Privacy - Contacts Usage Description  -  是否允许使用你的通讯录?

iOS - 获取通讯录_第1张图片


三、效果

iOS - 获取通讯录_第2张图片




iOS - 获取通讯录_第3张图片



四、联系人相关数据

       //获取当前联系人姓氏
        NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
        //获取当前联系人中间名
        NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty));
        //获取当前联系人的名字前缀
        NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonPrefixProperty));

        //获取当前联系人的名字后缀
        NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonSuffixProperty));

        //获取当前联系人的昵称
        NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty));

        //获取当前联系人的名字拼音
        NSString *firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty));

        //获取当前联系人的姓氏拼音
        NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonLastNamePhoneticProperty));

        //获取当前联系人的中间名拼音
        NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNamePhoneticProperty));

        //获取当前联系人的公司
        NSString *organization=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonOrganizationProperty));

        //获取当前联系人的职位
        NSString *job=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonJobTitleProperty));

        //获取当前联系人的部门
        NSString *department=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonDepartmentProperty));

        //获取当前联系人的生日
        NSString *birthday=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonBirthdayProperty));

        //获取当前联系人的备注
        NSString *notes=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty));

        //获取创建当前联系人的时间 注意是NSDate
        NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonCreationDateProperty));

        //获取最近修改当前联系人的时间
        NSDate *alterTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonModificationDateProperty));

        //获取地址
        ABMultiValueRef address = ABRecordCopyValue(people, kABPersonAddressProperty);
        for (int j=0; j




感谢观看,学以致用更感谢~






你可能感兴趣的:(iOS-基础)