获取手机所有联系人及搜索
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);
}
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);
}
相关文章推荐
- iPhone/iOS开启个人热点的相关位置调整小结
- 实现按钮拖动到底部,与底部按钮互换的功能
- Spark调度系列-----5.Spark task和Stage的跳过执行(ui显示task和stage skipped)
- SSL与TLS的区别以及介绍
- 总结一下打字
- (工具准备)Vim
- android-async-http
- iOS 播放视频时隐藏屏幕上方的状态栏
- 杭电ACM1312(dfs)
- 标签页,程序开始导航
- Shell集合运算
- c++学习日记2015.12.22
- Objective-C:Foundation框架-概述
- 62.在Android Studio中使用Git进行版本管理
- Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
- 查询某一段时间内的所有天或月
- Libgdx学习笔记:UI之技能冷却按钮(新版)
- onActivityResult 取不到返回值的问题
- androidannotions开发框架在Eclipse中的配置
- 63.友盟分享的那些坑