IOS中获取本地通讯录联系人以及汉字首字母排序
发布时间 - 2026-01-10 21:53:25 点击率:次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实现从通讯录中选择联系人
# 你也
# 可以用
# 希望能
# 不存在
# 很重要
# 英语
# 中去
# 谢谢大家
# 就为
# 新建一个
# 应该是
# 首字母
# 加载
# 中排
# lastName
# ABRecordCopyValue
# kABPersonFirstNameProperty
# kABPersonLastNameProperty
# NSLog
# CFArrayGetValueAtIndex
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
如何在IIS7中新建站点?详细步骤解析
如何基于云服务器快速搭建网站及云盘系统?
香港网站服务器数量如何影响SEO优化效果?
Laravel如何自定义分页视图?(Pagination示例)
学生网站制作软件,一个12岁的学生写小说,应该去什么样的网站?
新三国志曹操传主线渭水交兵攻略
PHP怎么接收前端传的文件路径_处理文件路径参数接收方法【汇总】
Laravel怎么生成二维码图片_Laravel集成Simple-QrCode扩展包与参数设置【实战】
香港服务器租用费用高吗?如何避免常见误区?
为什么php本地部署后css不生效_静态资源加载失败修复技巧【技巧】
Laravel路由怎么定义_Laravel核心路由系统完全入门指南
Laravel如何升级到最新版本?(升级指南和步骤)
网站建设保证美观性,需要考虑的几点问题!
中山网站制作网页,中山新生登记系统登记流程?
JavaScript如何实现倒计时_时间函数如何精确控制
JavaScript如何实现继承_有哪些常用方法
如何解决hover在ie6中的兼容性问题
Laravel如何将应用部署到生产服务器_Laravel生产环境部署流程
Laravel的Blade指令怎么自定义_创建你自己的Laravel Blade Directives
HTML透明颜色代码怎么让图片透明_给img元素加透明色的技巧【方法】
百度输入法ai组件怎么删除 百度输入法ai组件移除工具
Laravel如何使用Scope本地作用域_Laravel模型常用查询逻辑封装技巧【手册】
如何快速登录WAP自助建站平台?
,南京靠谱的征婚网站?
Laravel N+1查询问题如何解决_Eloquent预加载(Eager Loading)优化数据库查询
悟空识字怎么关闭自动续费_悟空识字取消会员自动扣费步骤
Laravel如何处理JSON字段_Eloquent原生JSON字段类型操作教程
高端智能建站公司优选:品牌定制与SEO优化一站式服务
b2c电商网站制作流程,b2c水平综合的电商平台?
Laravel如何使用Gate和Policy进行权限控制_Laravel权限判定与策略规则配置
网站建设整体流程解析,建站其实很容易!
Laravel如何设置定时任务(Cron Job)_Laravel调度器与任务计划配置
Laravel如何安装Breeze扩展包_Laravel用户注册登录功能快速实现【流程】
如何用VPS主机快速搭建个人网站?
今日头条AI怎样推荐抢票工具_今日头条AI抢票工具推荐算法与筛选【技巧】
济南网站建设制作公司,室内设计网站一般都有哪些功能?
javascript中数组(Array)对象和字符串(String)对象的常用方法总结
详解Oracle修改字段类型方法总结
Laravel如何实现全文搜索功能?(Scout和Algolia示例)
Laravel用户密码怎么加密_Laravel Hash门面使用教程
教你用AI将一段旋律扩展成一首完整的曲子
西安市网站制作公司,哪个相亲网站比较好?西安比较好的相亲网站?
Laravel DB事务怎么使用_Laravel数据库事务回滚操作
Swift中swift中的switch 语句
如何在阿里云服务器自主搭建网站?
实现点击下箭头变上箭头来回切换的两种方法【推荐】
网页设计与网站制作内容,怎样注册网站?
iOS正则表达式验证手机号、邮箱、身份证号等
Laravel怎么实现观察者模式Observer_Laravel模型事件监听与解耦开发【指南】
使用豆包 AI 辅助进行简单网页 HTML 结构设计

