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 *)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中获取本地通讯录联系人以及汉字首字母排序的更多相关文章
- oracle中检索结果汉字首字母排序详解
今天写需求,要求将结果按照成本中心首字母排序,平且空放在最前面. 进入正题: 1.使用oracle自带的函数: 按照首字母排序:nlssort(xxx,'NLS_SORT=SCHINESE_PINYI ...
- js对汉字首字母排序
var city = ["北京","天津","上海","重庆","杭州"];city.sort(fu ...
- mysql根据汉字首字母排序[转]
select areaName from area order by convert(areaName USING gbk) COLLATE gbk_chinese_ci asc 说明 ...
- 获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。
package com.lixu.tongxunlu; import java.util.ArrayList; import com.lixu.tongxunlu.PinnedSectionListV ...
- 在iOS中获取UIView的所有层级结构 相关
在iOS中获取UIView的所有层级结构 应用场景 在实际 iOS 开发中,很多时候都需要知道某个 UI 控件中包含哪些子控件,并且分清楚它们的层级结构和自个的 frame 以及 bounds ,以便 ...
- 向Android模拟器中批量导入通讯录联系人
使用adb命令向Android模拟器中批量导入通讯录联系人的方法: 使用adb提供的命令, 可以非常方便地从PC中将通讯录批量导入android模拟器中. 首先要先准备好固定格式的vcf文件, 该文件 ...
- IOS中获取各种文件的路径介绍及方法
IOS中获取各种文件的目录路径的方法 技术交流新QQ群:414971585 iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储一般放在什么位置,得到模拟器的路径的简单方式是什么. docum ...
- IOS中获取各个文件的目录路径的方法和NSFileManager类
转自:http://blog.sina.com.cn/s/blog_5fb39f910101di92.html IOS中获取各种文件的目录路径的方法 iphone沙箱模型的有四个文件夹,分别是什么,永 ...
- 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。
package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...
随机推荐
- [django] 利用多线程增加异步任务
看到django异步大家的反应应该是celery这种消息队列组件,现在用的最多的最推荐的也是这种方式.然而我这需求就是请求来了,执行一个小程序,但是又不能确定这个小程序啥时候执行完,响应又要及时,丢给 ...
- [ExtJS5学习笔记]第二十九节 sencha ext js 5.1.0中动态更换皮肤主题
本文地址:http://blog.csdn.net/sushengmiyan/article/details/42016107 本文作者:sushengmiyan ------------------ ...
- 【安卓开发】Facebook工程师是如何改进他们Android客户端的
原文出处: Facebook 译文出处:penkzhou 欢迎分享原创到伯乐头条 作为世界上最大的社交网络,Facebook的Android客户端面临着各种各样的使用环境(地理环境.Andro ...
- linux下的清屏命令
Linux下有两个清屏命令: clear 这个命令将会刷新屏幕,系统的操作是让终端显示页向后翻了一页,如果向上滚动屏幕还可以看到之前的操作信息.一般都会使用这个命令. reset 这个命令将完全刷新终 ...
- Dynamics CRM2015 on-premises直接升级Dynamics CRM2016 on-premises
Dynamics crm2016 on-premises版本已与12月14日开放下载,下载地址:https://www.microsoft.com/zh-cn/download/details.asp ...
- 详解EBS接口开发之库存事务处理采购接收--补充
除了可以用 详解EBS接口开发之库存事务处理采购接收的方法还可以用一下方法,不同之处在于带有批次和序列控制的时候实现方式不同 The script will load records into ...
- Android游戏开发之SurfaceView的使用-android学习之旅(五)
SurfaceView和View的区别 View是在ui主线程中,直接响应用户的操作,以及任务的分发,但是任务比较复杂会出现阻塞. SurfaceView则不会出现这种问题,以为它直接从内存等取得图像 ...
- J2EE进阶(十一)SSH框架整合常见问题汇总(二)
org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of cn. ...
- DAA和CMAC
数据认证算法(DAA) Data Authentication Algorithm DAA建立在DES之上,该算法比较陈旧,人们已经发现了这个算法的安全弱点,目前已经被废止. DAA采用DES运算的 ...
- Http协议处理器——Http11Processor
Http11Processor组件提供了对Http协议通信的处理,包括对套接字的读取过滤.对http协议的解析并封装成请求对象.http响应对象的生成.套接字的过滤写入等等操作. 喜欢研究java的同 ...