获取手机所有联系人及搜索

_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];

    _searchBar.placeholder = @"搜索";

    _searchBar.delegate = self;

    _searchBar.barStyle = UIBarStyleDefault;

    _tableView.tableHeaderView = _searchBar;


#pragma mark 输入文本实时更新时调用

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

{

    [_resultArr removeAllObjects];

    if (searchText.length != 0) {

        for (int i = 0; i < _dataArr.count; i++) {

            PhoneModel *book = [_dataArr objectAtIndex:i];

            NSRange range = [book.name rangeOfString:searchText];

            NSRange ran = [book.tel rangeOfString:searchText];

            if (range.length || ran.length) {

                [_resultArr addObject:_dataArr[i]];

            }

        }

        [_tableView reloadData];

    }else{

        for (int i = 0; i<_dataArr.count; i++) {

            [_resultArr addObject:_dataArr[i]];

        }

        [_tableView reloadData];

    }

}

#pragma mark 点击search时调用

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{

    

    [_searchBar endEditing:YES];

}


- (void)huoquLianxiren{

    if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) {

        NSLog(@"禁止访问联系人");

        //创建一个警告框

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"联系人已被禁止访问" message:@"请在设置中设置允许访问" preferredStyle:(UIAlertControllerStyleAlert)];

        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {

            

        }];

        [alertController addAction:okAction];

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

        return;

    }

    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {

        if (granted) {

            [self filterContentForSearchText:@""];

        }

    });

    CFRelease(addressBook);

}

- (void)filterContentForSearchText:(NSString *)searchText{

    

    if (ABAddressBookGetAuthorizationStatus() != kABAuthorizationStatusAuthorized) {

        NSLog(@"禁止访问联系人");

        return;

    }

    CFErrorRef error = NULL;

    

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    

//    if ([searchText length] == 0) {//查询所有

//        addressBookTemp = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));

//    }else{//按条件查询

//        CFStringRef cfSearchText = (CFStringRef)CFBridgingRetain(searchText);

//        addressBookTemp = CFBridgingRelease(ABAddressBookCopyPeopleWithName(addressBook, cfSearchText));

//        CFRelease(cfSearchText);

//    }

    CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

    CFIndex number = ABAddressBookGetPersonCount(addressBook);

    for (NSInteger i = 0; i < number; i++) {

        PhoneModel *pm = [[PhoneModel alloc] init];

        ABRecordRef  people = CFArrayGetValueAtIndex(allLinkPeople, i);

        NSString*firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));

        NSString*lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));

        if (lastName == NULL) {

            pm.name = firstName;

        }

        if (firstName == NULL) {

            pm.name = lastName;

        }

        if (lastName!= NULL && firstName != NULL) {

            pm.name = [NSString stringWithFormat:@"%@%@",lastName,firstName];

        }

        

//        NSLog(@"name   %@",[NSString stringWithFormat:@"%@%@",lastName,firstName]);

        NSMutableArray * phoneArr = [[NSMutableArray alloc]init];

        ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty);

        for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) {

            [phoneArr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))];

        }

//        NSLog(@"phonenum   %@",phoneArr[0]);

        pm.tel = phoneArr[0];

        pm.isChange = NO;

        [_dataArr addObject:pm];

    }

    for (int i = 0; i<_dataArr.count; i++) {

        [_resultArr addObject:_dataArr[i]];

    }

    [_tableView reloadData];

    CFRelease(addressBook);

//    NSLog(@"%ld",number);

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值