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 ...
随机推荐
- Java并发框架——什么是AQS框架
什么是AQS框架 1995年sun公司发布了第一个java语言版本,可以说从jdk1.1到jdk1.4期间java的使用主要是在移动应用和中小型企业应用中,在此类领域中基本不用设计大型并发场景,当然也 ...
- Android初级教程:shape的基本用法
转载本文请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203347 点击打开链接 在自定义进度条之前,先来学习一下shape的用 ...
- mysql 字符集查看 设定
(1) 最简单的修改方法,就是修改mysql的my.ini文件中的字符集键值, 如 default-character-set = utf8 character_set_server = utf8 修 ...
- JS 遍历对象 jQuery遍历对象
jquery for 循环遍历对象的属性: //对象的定义如下: var person={id:"1",name:"springok",age:25}; for ...
- Citrix 桌面虚拟化解决方案与VMware桌面虚拟化解决方案对比
通过 XenDesktop 和 FlexCast为各种场景交付虚拟桌面 企业桌面面临的问题 为每个用户提供安全高效的桌面环境是几乎所有公司或组织的基本要求.如果用户无法使用他们的桌面或应用程序,公司就 ...
- 当图片验证码遇上JSP
今天看到了一个关于使用JSP方式生成图片验证码 的小例子,感觉真的是很不错,拿来分享一下. 原理 对于图片验证码,我们在审查元素的时候会方便的看出是<img src="#" ...
- 让 Google Test 出错时断点
Google Test 缺省是出错退出. 如果最后的出错行在系统库中,那就没什么帮助. 如果是调试运行,直接退出根本就不知道哪里出错了. 后来添加了一个运行参数: --gtest_break_on_f ...
- Android初级教程IP拨号器初识广播接受者
需求:输入ip号码并且保存在本地,监听打电话广播,如果电话号码以0开头,则加上ip区号拨打. 首先定义一个页面布局: <LinearLayout xmlns:android="http ...
- 【Android应用开发】 Android 崩溃日志 本地存储 与 远程保存
示例代码下载 : http://download.csdn.net/detail/han1202012/8638801; 一. 崩溃日志本地存储 1. 保存原理解析 崩溃信息本地保存步骤 : -- 1 ...
- Android的搜索框SearchView的用法-android学习之旅(三十九)
SearchView简介 SearchView是搜索框组件,他可以让用户搜索文字,然后显示.' 代码示例 这个示例加了衣蛾ListView用于为SearchView增加自动补全的功能. package ...