调用系统通讯录+条件查询联系人

#import "ZYViewController.h"
#import "ZYPeople.h"
#import "ChineseToPinyin.h"//第三方库

@interface ZYViewController ()

@end

@implementation ZYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _tableView.delegate =self;
    _tableView.dataSource =self;
    
    _array =[[NSMutableArray alloc]init];
    _resultArray=[[NSMutableArray alloc]init];
    _dic =[[NSMutableDictionary alloc]init];
    
    UISearchBar *searchBar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, 320, 44)];
    [searchBar setScopeButtonTitles:[[NSArray alloc]initWithObjects:@"按首字母查询",@"按电话查询", nil]];
    searchBar.delegate =self;
    [self.view addSubview:searchBar];
    
    UISearchDisplayController *DisplayController=[[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
    
    DisplayController.searchResultsDataSource =self;
    DisplayController.searchResultsDelegate =self;
    DisplayController.delegate =self;
    
 
    ABAddressBookRequestAccessWithCompletion(nil, ^(bool granted, CFErrorRef error) {
        if (granted ==YES) {
            NSLog(@"获取授权成功");
            //创建通讯录对象:
            ABAddressBookRef  AddressBook=ABAddressBookCreateWithOptions(nil , nil);
           //数组里储存的是所有联系人的信息:
            NSArray *arr =(NSArray *)ABAddressBookCopyArrayOfAllPeople(AddressBook);
            for (int i=0 ; i<arr.count; i++) {
                ZYPeople *p =[[ZYPeople alloc]init];
                ABRecordRef person =arr[i];
                NSString *name =(NSString*)ABRecordCopyCompositeName(person);
                p.name =name;
                //获得用户所有电话:
                ABMultiValueRef ref =ABRecordCopyValue(person, kABPersonPhoneProperty);
                //获取联系人第一个电话号:
                NSString *phone =ABMultiValueCopyValueAtIndex(ref, 0);
                p.people =[self trimSteing:phone];
                [_array addObject:p];
                [p release];
                }
            for (ZYPeople *p in _array) {
                NSString *name =[ChineseToPinyin pinyinFromChiniseString:p.name];
                NSString *fristLetter=[name substringWithRange:NSMakeRange(0, 1)];
                if (![_dic objectForKey:fristLetter]) {
                    NSMutableArray *arr =[NSMutableArray array];
                    [_dic setObject:arr forKey:fristLetter];
                }
                [[_dic objectForKey:fristLetter]addObject:p];
            }
            NSLog(@"%@",[_dic allKeys]);
            //isMainThread是否在主线程
            NSLog(@"%d",[NSThread isMainThread]);
            [self performSelectorOnMainThread:@selector(reloadTanleView) withObject:nil waitUntilDone:YES];
            
        }else{
            NSLog(@"请求授权失败");
        }
    });
  //  [_tableView registerNib:[UINib nibWithNibName:@"ZYTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
  //  [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
-(void)reloadTanleView{
    
    [_tableView reloadData];
}
-(NSString *)trimSteing:(NSString*)String{
    
    NSMutableString *mString =[NSMutableString stringWithString:String];
    NSCharacterSet *set =[NSCharacterSet decimalDigitCharacterSet];
    for (int i=0; i<mString.length; i++) {
        NSString *str =[mString substringWithRange:NSMakeRange(i, 1)];
        NSCharacterSet *sSet =[NSCharacterSet characterSetWithCharactersInString:str];
        if ([set isSupersetOfSet:sSet]) {
            
        }else{
            [mString deleteCharactersInRange:NSMakeRange(i, 1)];
            i--;
        }
    }
    return mString;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    
    [_resultArray removeAllObjects];
    if (controller.searchBar.selectedScopeButtonIndex ==0) {
        for (ZYPeople *p  in _array) {
            NSString *name =[ChineseToPinyin pinyinFromChiniseString:p.name];
            if ([name hasPrefix:[searchString uppercaseString]]) {
                [_resultArray addObject:p];
            }
        }
    }
    else{
        for (ZYPeople *p in _array) {
            if ([[p.people uppercaseString ] hasPrefix:[searchString uppercaseString]]) {
                [_resultArray addObject:p];
            }
        }
    }
    return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{
    [self searchDisplayController:controller shouldReloadTableForSearchString:controller.searchBar.text];
    
    return YES;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (tableView ==_tableView) {
        return [[_dic allKeys]count];
    }else{
        return 1;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView ==_tableView) {
        NSArray *sortArr =[[_dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
        NSArray *arr=[_dic objectForKey:[sortArr objectAtIndex:section]];
        return arr.count ;
    }
    
    return _resultArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier [email protected]"cell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell =[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier]autorelease];
    }
    if (tableView ==_tableView) {
        NSArray *sortArr =[[_dic allKeys] sortedArrayUsingSelector:@selector(compare:)];
        NSArray*arr =[_dic objectForKey:[sortArr objectAtIndex:indexPath.section]];
        ZYPeople *p =arr[indexPath.row];
        cell.textLabel.text =p.name;
        cell.detailTextLabel.text =p.people;
        
    } else
    {
        ZYPeople *p = [_resultArray objectAtIndex:indexPath.row];
        cell.textLabel.text = p.name;
        cell.detailTextLabel.text = p.people;
    }

return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (tableView ==_tableView) {
        NSArray*sortArr =[[_dic allKeys]sortedArrayUsingSelector:@selector(compare:)];
        return [sortArr objectAtIndex:section];
    }else{
    return nil;
    }
}
-(NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    if (tableView ==_tableView) {
        NSArray *sortArr =[[_dic allKeys]sortedArrayUsingSelector:@selector(compare:)];
        return sortArr;
        
    }
    return nil;
    
}

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
@end

时间: 2024-07-31 14:35:14

调用系统通讯录+条件查询联系人的相关文章

怎么调用系统通讯录并向被选中联系人发送短信

每做一个项目都会有收获,前提是要在这个项目上付出努力的! 好吧,如今讲一下:怎么通过调用系统通讯录,当你点击联系人姓名时,跳转到向其发送短信的页面<收件人是被点中的联系人,短信已自己主动编辑>. 看看图片效果: 以下看一下具体代码: Uri result = data.getData(); String phoneName = getPhoneContacts(result); Log.d("phone", "---------->phoneName==&q

iOS --调用系统通讯录

// 调用系统通讯录需要遵循两个代理ABPeoplePickerNavigationControllerDelegate,UINavigationControllerDelegate 相关类为ABPeoplePickerNavigationController // 系统通讯录自带导航栏,所有要model出来 // 初始化 ABPeoplePickerNavigationController *peoplePicker = [[ABPeoplePickerNavigationController

iOS开发--调用系统通讯录界面

今天写代码遇到了要调用系统通讯录,看了一些博客发现写的都是获取通讯录的内容,而不是调用系统的界面. 分享一下自己写的代码 第一步:引入 #import <AddressBook/AddressBook.h> #import <AddressBookUI/AddressBookUI.h> 第二步:添加点击事件 创建一个通讯录界面 并以present的方式跳转 #pragma mark -- IBAction - (IBAction)buttonClicked:(id)sender {

Android 调用系统通讯录

这个过程有三步:1)许可向你的应用程序的manifest中添加读取通讯录数据的许可  <uses-permission android:name="android.permission.READ_CONTACTS" />    <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 2)调用Contact Picker在你的activity中,创建一个Inten

iOS7 iOS8 调用系统通讯录界面

#pragma mark--选取手机联系人 -(void)selectPeople{ //这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录 int __block tip=0; //声明一个通讯簿的引用 ABAddressBookRef addBook =nil; //创建通讯簿的引用 addBook=ABAddressBookCreateWithOptions(NULL, NULL); //创建一个出事信号量为0的信号 dispatch_semaphore_t sema=dispat

IOS 获取系统通讯录中的联系人信息

- (IBAction)getAllContactFromSystem { ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NULL); ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) { //取得通讯录访问授权 ABAuthorizationStatus authorization= ABAddressBookGet

iOS开发--系统通讯录的访问与添加联系人

公司项目有访问通讯录的需求,所以开始了探索之路.从开始的一无所知,到知识的渐渐清晰.这一切要感谢广大无私分享的 “coder”,注:我是尊称的语气! 苹果提供了访问系统通讯录的框架,以便开发者对系统通讯录进行操作.(此demo为纯代码),想要访问通讯录,需要添加AddressBookUI.framework和AddressBook.framework两个框架,添加的地点这里就不在赘述了.在控制器内部首先import两个头文件,<AddressBook/AddressBook.h> 和 <

iOS -------- 应用程序引用系统通讯录

转自:http://www.cnblogs.com/ygm900/p/3472288.html 由于ios系统对用户隐私的控制,第三方应用程序只能通过苹果官方接口调用系统通讯录,不能像android那样直接操作通讯录数据库.     一般地,使用系统自带通讯录的方法有两种,一种是直接将整个通讯录引入到应用程序,另一种是逐条读取通讯录中的每一条联系人信息.下面我们就一一详解. 1 直接引用整个通讯录 使用的类:ABPeoplePickerNavigationController方法: 在Local

Android-数据存储(Content Provider ,调用系统 联系人 ContentProvider实现查询和增加联系人)

1.回顾 上篇学习了 安卓的 文件存储 ,包括  存储在 sd卡上: 2.重点 (1)了解 四大组件之一 Content Provider (2)实现 查询通信录中 联系人 (3)实现 新增联系人到通信录 3.介绍 (1) ContentProvider相对于其它的方式比较复杂,当然其功能相对于其它的方式也是革命性的改变. (2)它能够实现跨应用之间的数据操作. (3)利用ContentResolver对象的delete.update.insert.query等方法去操作 ContentProv