_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);
}