您的位置:首页 > 其它

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

2015-12-22 17:46 405 查看
_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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: