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

 更新时间:2016年12月16日 15:24:59   投稿:lqh  
这篇文章主要介绍了IOS中获取本地通讯录联系人以及汉字首字母排序的相关资料,需要的朋友可以参考下

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

/*** 加载本地联系人*/ 
- (void)loadLocalContacts 
{ 
  //新建一个通讯录类 
  ABAddressBookRef addressBooks = nil; 
   
  if (DeviceVersion < 6.0) { 
    addressBooks = ABAddressBookCreate(); 
  } else { 
    addressBooks = ABAddressBookCreateWithOptions(NULL, NULL); 
    //获取通讯录权限 
    dispatch_semaphore_t sema = dispatch_semaphore_create(0); 
    ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);}); 
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 
    dispatch_release(sema); 
  } 
   
  //判断授权状态 
  if (ABAddressBookGetAuthorizationStatus()!=kABAuthorizationStatusAuthorized) { 
    return ; 
  } 
   
  //获取通讯录中的所有人 
  CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks); 
  //通讯录中人数 
  CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks); 
  NSMutableArray *persons = [[NSMutableArray alloc] init]; 
  for (int i = 0; i < nPeople; i++) { 
    //获取个人 
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i); 
    //获取个人名字 
    NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
    NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 
    NSMutableString *name = [[NSMutableString alloc] init]; 
    if (firstName == nil && lastName == nil) { 
      NSLog(@"名字不存在的情况"); 
      name = nil; 
    } 
    if (lastName) { 
      [name appendString:lastName]; 
    } 
    if (firstName) { 
      [name appendString:firstName]; 
    } 
     
    ABMultiValueRef tmlphone = ABRecordCopyValue(person, kABPersonPhoneProperty); 
    NSString *telphone = (NSString *)ABMultiValueCopyValueAtIndex(tmlphone, 0); 
    if (telphone != nil) { 
      telphone = [telphone stringByReplacingOccurrencesOfString:@"-" withString:@""]; 
      NSString *title = [NSString stringWithFormat:@"%@(%@)",name,telphone]; 
      [persons addObject:title]; 
    } 
  } 
   
  //对联系人进行分组和排序 
  UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation]; 
  NSInteger highSection = [[theCollation sectionTitles] count]; //中文环境下返回的应该是27,是a-z和#,其他语言则不同 
   
  //_indexArray 是右侧索引的数组,也是secitonHeader的标题 
  _indexArray = [[NSMutableArray alloc] initWithArray:[theCollation sectionTitles]]; 
   
  NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:highSection]; 
  //初始化27个空数组加入newSectionsArray 
  for (NSInteger index = 0; index < highSection; index++) { 
    NSMutableArray *array = [[NSMutableArray alloc] init]; 
    [newSectionsArray addObject:array]; 
    [array release]; 
  } 
   
  for (NSString *p in persons) { 
    //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11 
    NSInteger sectionNumber = [theCollation sectionForObject:p collationStringSelector:@selector(getFirstLetter)]; 
    //把name为“林丹”的p加入newSectionsArray中的第11个数组中去 
    NSMutableArray *sectionNames = newSectionsArray[sectionNumber]; 
    [sectionNames addObject:p]; 
  } 
   
  for (int i = 0; i < newSectionsArray.count; i++) { 
    NSMutableArray *sectionNames = newSectionsArray[i]; 
    if (sectionNames.count == 0) { 
      [newSectionsArray removeObjectAtIndex:i]; 
      [_indexArray removeObjectAtIndex:i]; 
      i--; 
    } 
  } 
   
  //_contacts 是联系人数组(确切的说是二维数组) 
  self.contacts = newSectionsArray; 
  [newSectionsArray release]; 
   
  [self.tableView reloadData]; 
} 

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

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
  return self.contacts.count; 
} 
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
  return [self.contacts[section] count]; 
} 
 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
  static NSString *identifier = @"contactCell"; 
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
  if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 
  } 
   
  cell.imageView.image = [UIImage imageNamed:@"default_head"]; 
  cell.textLabel.text = [self.contacts objectAtIndex:indexPath.section][indexPath.row]; 
  return cell; 
} 
 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
  return [_indexArray objectAtIndex:section]; 
} 
 
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView 
{ 
  return _indexArray; 
} 
 
//索引列点击事件 
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index 
{ 
  return index; 
} 

还有两个很重要的方法:

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

 NSString *ret = @""; 
  if (![self canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语 
    if ([[self letters] length]>2) { 
      ret = [[self letters] substringToIndex:1]; 
    } 
  } 
  else { 
    ret = [NSString stringWithFormat:@"%c",[self characterAtIndex:0]]; 
  } 
  return ret; 
} 

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

- (NSString *)letters{ 
  NSMutableString *letterString = [NSMutableString string]; 
  int len = [self length]; 
  for (int i = 0;i < len;i++) 
  { 
    NSString *oneChar = [[self substringFromIndex:i] substringToIndex:1]; 
    if (![oneChar canBeConvertedToEncoding:NSASCIIStringEncoding]) { 
      NSArray *temA = makePinYin2([oneChar characterAtIndex:0]); 
      if ([temA count]>0) { 
        oneChar = [temA objectAtIndex:0]; 
      } 
    } 
    [letterString appendString:oneChar]; 
  } 
  return letterString; 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:

相关文章

  • IOS视图控制器的生命周期实例详解

    IOS视图控制器的生命周期实例详解

    这篇文章主要介绍了IOS视图控制器的生命周期实例详解的相关资料,需要的朋友可以参考下
    2017-04-04
  • iOS判断运营商类型的实现方法

    iOS判断运营商类型的实现方法

    下面小编就为大家带来一篇iOS判断运营商类型的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • 关于iOS中的各种颜色设置总结大全(推荐)

    关于iOS中的各种颜色设置总结大全(推荐)

    这篇文章主要给大家介绍了关于iOS中颜色设置的相关资料,其中包括导航栏、状态栏、Tabbar、Button、TextField、AttributedString和通用部分的颜色设置方法示例,对大家具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。
    2017-09-09
  • iOS应用开发中运用设计模式中的组合模式的实例解析

    iOS应用开发中运用设计模式中的组合模式的实例解析

    这篇文章主要介绍了iOS应用开发中运用设计模式中的组合模式的实例解析,示例代码为传统的Objective-C,需要的朋友可以参考下
    2016-03-03
  • iOS中状态栏的基本使用方法汇总

    iOS中状态栏的基本使用方法汇总

    在iOS开发过程中,经常会设置状态栏的样式,所以下面这篇文章主要给大家介绍了关于iOS中状态栏的基本使用的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-07-07
  • iOS逆向工程使用dumpdecrypted工具给App脱壳

    iOS逆向工程使用dumpdecrypted工具给App脱壳

    这篇文章主要介绍了iOS逆向工程使用dumpdecrypted工具给App脱壳的相关资料,本文图文并茂给大家介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
    2016-09-09
  • iOS开发中最基本的位置功能实现示例

    iOS开发中最基本的位置功能实现示例

    这篇文章主要介绍了iOS开发中最基本的位置功能实现示例,需要的朋友可以参考下
    2015-09-09
  • iOS简单抽屉效果的实现方法

    iOS简单抽屉效果的实现方法

    这篇文章主要为大家详细介绍了iOS简单抽屉效果的实现方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-08-08
  • IOS开发之CocoaPods安装和使用教程

    IOS开发之CocoaPods安装和使用教程

    CocoaPods应该是iOS最常用最有名的类库管理工具了,通过cocoaPods,只需要一行命令就可以完全解决,当然前提是你必须正确设置它。重要的是,绝大部分有名的开源类库,都支持CocoaPods。所以,作为iOS程序员的我们,掌握CocoaPods的使用是必不可少的基本技能了。
    2014-09-09
  • objective-c实现点到直线的距离及与垂足的交点

    objective-c实现点到直线的距离及与垂足的交点

    这篇文章主要给大家介绍了利用objective-c实现点到直线的距离及与垂足的交点的相关资料,文中给出了详细的实现思路和实现代码,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-04-04

最新评论