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

#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 =@"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

 
posted @ 2015-11-12 16:25  婆娑年华  阅读(88)  评论(0编辑  收藏  举报