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

所属分类: 软件编程 / IOS 阅读数: 1693
收藏 0 赞 0 分享

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中UITableView的性能优化

最近在微博上看到一个很好的开源项目,是关于如何优化UITableView的,加上正好最近也在优化项目中的类似朋友圈功能这块,思考了很多关于UITableView的优化技巧,所以决定详细的整理下对优化UITableView的理解,需要的朋友们可以参考借鉴。
收藏 0 赞 0 分享

IOS开发中禁止NavigationController的向右滑动返回

这篇文章主要介绍了IOS开发中禁止NavigationController的向右滑动返回的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS实现微信/QQ显示最近拍摄图片的功能实例代码

如果你刚刚拍摄了图片,在使用微信/QQ发生消息时会显示“你可能要发送的图片”,这个功能非常人性化,怎么实现的呢?下面小编给大家分享iOS实现微信/QQ显示最近拍摄图片的功能实例代码,一起看看吧
收藏 0 赞 0 分享

iOS实现动态自适应标签

这篇文章主要为大家详细介绍了iOS动态自适应标签的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS实现图片存在本地、再从本地获取图片的功能

本文主要介绍了iOS实现图片存在本地、再从本地获取图片的功能的代码。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS视频录制(或选择)压缩及上传功能(整理)

最新做的一个功能涉及到了视频的录制、压缩及上传功能,经过大神的一番教导,终于倒腾清楚了,今天小编把问题经过记录一下分享到脚本之家平台,供大家参考
收藏 0 赞 0 分享

iOS中打包上传常见的错误与解决办法

关于打包上传至AppStore,大家都认为是最后一步了,其实到了这里往往会遇到很多的坑。对于踩过的坑我不想再踩第二遍,所以在此将我遇到的所有奇葩问题在此做一个记录,当作对自己的一个提醒,同时也分享给给位,需要的朋友可以参考下。
收藏 0 赞 0 分享

解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法

这篇文章主要介绍了关于解决Xcode 8构建版本iTunes Connect获取不到应用程序状态的办法,需要的朋友可以参考下
收藏 0 赞 0 分享

Objective-C实现身份证验证的方法示例

这篇文章主要给大家分享了Objective-C实现身份证验证的方法,文中给出了详细的示例代码,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

ios启动页强制竖屏(进入App后允许横屏与竖屏)

最近工作遇到这样一个需要,当进入启动页需要强制竖屏,而进入APP后就允许横屏与竖屏,通过查找相关的资料找到了解决的方法,所以将实现的方法整理后分享出来,需要的朋友们可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享
查看更多