iOS中获取本地通讯录联系人以及汉字首字母排序

iOS中获取手机通讯录中的联系人信息:

 

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /***  加载本地联系人*/  
  2. - (void)loadLocalContacts  
  3. {  
  4.     //新建一个通讯录类  
  5.     ABAddressBookRef addressBooks = nil;  
  6.       
  7.     if (DeviceVersion < 6.0) {  
  8.         addressBooks = ABAddressBookCreate();  
  9.     } else {  
  10.         addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);  
  11.         //获取通讯录权限  
  12.         dispatch_semaphore_t sema = dispatch_semaphore_create(0);  
  13.         ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});  
  14.         dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  
  15.         dispatch_release(sema);  
  16.     }  
  17.       
  18.     //判断授权状态  
  19.     if (ABAddressBookGetAuthorizationStatus()!=kABAuthorizationStatusAuthorized) {  
  20.         return ;  
  21.     }  
  22.       
  23.     //获取通讯录中的所有人  
  24.     CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);  
  25.     //通讯录中人数  
  26.     CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);  
  27.     NSMutableArray *persons = [[NSMutableArray alloc] init];  
  28.     for (int i = 0; i < nPeople; i++) {  
  29.         //获取个人  
  30.         ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);  
  31.         //获取个人名字  
  32.         NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);  
  33.         NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);  
  34.         NSMutableString *name = [[NSMutableString alloc] init];  
  35.         if (firstName == nil && lastName == nil) {  
  36.             NSLog(@"名字不存在的情况");  
  37.             name = nil;  
  38.         }  
  39.         if (lastName) {  
  40.             [name appendString:lastName];  
  41.         }  
  42.         if (firstName) {  
  43.             [name appendString:firstName];  
  44.         }  
  45.           
  46.         ABMultiValueRef tmlphone =  ABRecordCopyValue(person, kABPersonPhoneProperty);  
  47.         NSString *telphone = (NSString *)ABMultiValueCopyValueAtIndex(tmlphone, 0);  
  48.         if (telphone != nil) {  
  49.             telphone = [telphone stringByReplacingOccurrencesOfString:@"-" withString:@""];  
  50.             NSString *title = [NSString stringWithFormat:@"%@(%@)",name,telphone];  
  51.             [persons addObject:title];  
  52.         }  
  53.     }  
  54.       
  55.     //对联系人进行分组和排序  
  56.     UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];  
  57.     NSInteger highSection = [[theCollation sectionTitles] count]; //中文环境下返回的应该是27,是a-z和#,其他语言则不同  
  58.       
  59.     //_indexArray 是右侧索引的数组,也是secitonHeader的标题  
  60.     _indexArray = [[NSMutableArray alloc] initWithArray:[theCollation sectionTitles]];  
  61.       
  62.     NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:highSection];  
  63.     //初始化27个空数组加入newSectionsArray  
  64.     for (NSInteger index = 0; index < highSection; index++) {  
  65.         NSMutableArray *array = [[NSMutableArray alloc] init];  
  66.         [newSectionsArray addObject:array];  
  67.         [array release];  
  68.     }  
  69.       
  70.     for (NSString *p in persons) {  
  71.         //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11  
  72.         NSInteger sectionNumber = [theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)];  
  73.         //把name为“林丹”的p加入newSectionsArray中的第11个数组中去  
  74.         NSMutableArray *sectionNames = newSectionsArray[sectionNumber];  
  75.         [sectionNames addObject:p];  
  76.     }  
  77.       
  78.     for (int i = 0; i < newSectionsArray.count; i++) {  
  79.         NSMutableArray *sectionNames = newSectionsArray[i];  
  80.         if (sectionNames.count == 0) {  
  81.             [newSectionsArray removeObjectAtIndex:i];  
  82.             [_indexArray removeObjectAtIndex:i];  
  83.             i--;  
  84.         }  
  85.     }  
  86.       
  87.     //_contacts 是联系人数组(确切的说是二维数组)  
  88.     self.contacts = newSectionsArray;  
  89.     [newSectionsArray release];  
  90.       
  91.     [self.tableView reloadData];  
  92. }  

顺便把索引和tableView dataSource的代理方法也贴一下:

 

 

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  2. {  
  3.     return self.contacts.count;  
  4. }  
  5.   
  6. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  7. {  
  8.     return [self.contacts[section] count];  
  9. }  
  10.   
  11. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  12. {  
  13.     static NSString *identifier = @"contactCell";  
  14.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];  
  15.     if (cell == nil) {  
  16.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];  
  17.     }  
  18.       
  19.     cell.imageView.image = [UIImage imageNamed:@"default_head"];  
  20.     cell.textLabel.text = [self.contacts objectAtIndex:indexPath.section][indexPath.row];  
  21.     return cell;  
  22. }  
  23.   
  24. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  25. {  
  26.     return [_indexArray objectAtIndex:section];  
  27. }  
  28.   
  29. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView  
  30. {  
  31.     return _indexArray;  
  32. }  
  33.   
  34. //索引列点击事件  
  35. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index  
  36. {  
  37.     return index;  
  38. }  


还有两个很重要的方法:

 

下面这个方法是[theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)]; 是这里的p对象要实现的方法,我这里的p是NSString,你也可以用其他对象例如Person。

 

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. - (NSString *)getFirstLetter {  
  2.     NSString *ret = @"";  
  3.     if (![self canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语  
  4.         if ([[self letters] length]>2) {  
  5.             ret = [[self letters] substringToIndex:1];  
  6.         }  
  7.     }  
  8.     else {  
  9.         ret = [NSString stringWithFormat:@"%c",[self characterAtIndex:0]];  
  10.     }  
  11.     return ret;  
  12. }  

 

 

下面这个方法是NSString得类别方法

 

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
    1. - (NSString *)letters{  
    2.     NSMutableString *letterString = [NSMutableString string];  
    3.     int len = [self length];  
    4.     for (int i = 0;i < len;i++)  
    5.     {  
    6.         NSString *oneChar = [[self substringFromIndex:i] substringToIndex:1];  
    7.         if (![oneChar canBeConvertedToEncoding:NSASCIIStringEncoding]) {  
    8.             NSArray *temA = makePinYin2([oneChar characterAtIndex:0]);  
    9.             if ([temA count]>0) {  
    10.                 oneChar = [temA objectAtIndex:0];  
    11.             }  
    12.         }  
    13.         [letterString appendString:oneChar];  
    14.     }  
    15.     return letterString;  
    16. }  
posted @ 2016-10-09 11:58  点滴改变世界  阅读(287)  评论(0编辑  收藏  举报