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 *)getFirstLetter {
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中获取本地通讯录联系人以及汉字首字母排序的更多相关文章

  1. oracle中检索结果汉字首字母排序详解

    今天写需求,要求将结果按照成本中心首字母排序,平且空放在最前面. 进入正题: 1.使用oracle自带的函数: 按照首字母排序:nlssort(xxx,'NLS_SORT=SCHINESE_PINYI ...

  2. js对汉字首字母排序

    var city = ["北京","天津","上海","重庆","杭州"];city.sort(fu ...

  3. mysql根据汉字首字母排序[转]

    select  areaName  from area order by   convert(areaName USING gbk)   COLLATE   gbk_chinese_ci asc 说明 ...

  4. 获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。

    package com.lixu.tongxunlu; import java.util.ArrayList; import com.lixu.tongxunlu.PinnedSectionListV ...

  5. 在iOS中获取UIView的所有层级结构 相关

    在iOS中获取UIView的所有层级结构 应用场景 在实际 iOS 开发中,很多时候都需要知道某个 UI 控件中包含哪些子控件,并且分清楚它们的层级结构和自个的 frame 以及 bounds ,以便 ...

  6. 向Android模拟器中批量导入通讯录联系人

    使用adb命令向Android模拟器中批量导入通讯录联系人的方法: 使用adb提供的命令, 可以非常方便地从PC中将通讯录批量导入android模拟器中. 首先要先准备好固定格式的vcf文件, 该文件 ...

  7. IOS中获取各种文件的路径介绍及方法

    IOS中获取各种文件的目录路径的方法 技术交流新QQ群:414971585 iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. docum ...

  8. IOS中获取各个文件的目录路径的方法和NSFileManager类

    转自:http://blog.sina.com.cn/s/blog_5fb39f910101di92.html IOS中获取各种文件的目录路径的方法 iphone沙箱模型的有四个文件夹,分别是什么,永 ...

  9. 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。

      package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...

随机推荐

  1. Android Studio中Git的配置及协同开发

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51595096 本文出自:[openXu的博客] 目录: 一 Android Stutio配置 ...

  2. JAVA面向对象-----多态

    多态的概述 1:什么是多态 一个对象的多种状态 (老师)(员工)(儿子) 教师 a =老钟; 员工 b= 老钟; 2:多态体现 1:Father类 1:非静态成员变量x 2:静态成员变量y 3:非静态 ...

  3. Nginx的负载均衡 - 保持会话 (ip_hash)

    Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd 算法介绍 ip_hash算法的原理很简单,根据请求所属的客户端IP计算得到一个数值,然后把请求发往该数 ...

  4. Gradle 的Daemon配置

    最近升级到Android 2.2.2之后,运行之前的项目特别卡,基本上2分钟,好的时候1分半,查询了Android官网的说明说daemon能够加快编译.于是我也尝试开启Daemon. 在Windows ...

  5. EBS中的采购单据状态及其控制

     李  颖 (济南钢铁股份有限公司 装备部,山东 济南 250101) 摘 要:介绍了Oracle Purchasing模块中采购单据的管理与控制,结合实例,分析了各状态下可采取的控制活动及控制活 ...

  6. 05_MyBatis基于注解的开发

     要想开发基于注解的MyBatis应用.需要先写一个带有注解的接口. PersonDao.java的写法如下: package com.rl.dao; import java.util.List; ...

  7. Cocos2Dv3.4在AppDelegate中不返回的情况及解决

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们通常想在app启动的早期做一些事情,可能放在AppDeleg ...

  8. UNIX环境高级编程——网络编程常用函数及结构

    IP地址的转换 #include <arpa/inet.h>  int inet_aton(const char *strptr,                     struct i ...

  9. 在go中使用linked channels进行数据广播

    在go中使用linked channels进行数据广播 原文在这里(需FQ),为啥想要翻译这篇文章是因为在实际中也碰到过如此的问题,而该文章的解决方式很巧妙,希望对大家有用. 在go中channels ...

  10. 下载android5.0源码

    方法还是与之前我介绍的下载源码的方法一样,但是repo需要更新一下,否则可能会出现以下错误: type commit tag v1.12.16 tagger Conley Owens <cco3 ...