1. //get all people info from the address book
  2. ABAddressBookRef addressBook = ABAddressBookCreate();
  3. CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);//这是个数组的引用
  4. for(int i = 0; i<CFArrayGetCount(people); i++){
  5. //parse each person of addressbook
  6. ABRecordRef record=CFArrayGetValueAtIndex(people, i);//取出一条记录
  7. //以下的属性都是唯一的,即一个人只有一个FirstName,一个Organization。。。
  8. CFStringRef firstName = ABRecordCopyValue(record,kABPersonFirstNameProperty);
  9. CFStringRef lastName =  ABRecordCopyValue(record,kABPersonLastNameProperty);
  10. CFStringRef company = ABRecordCopyValue(record,kABPersonOrganizationProperty);
  11. CFStringRef department = ABRecordCopyValue(record,kABPersonDepartmentProperty);
  12. CFStringRef job = ABRecordCopyValue(record,kABPersonJobTitleProperty);
  13. //"CFStringRef"这个类型也是个引用,可以转成NSString*
  14. NSlog((NSString *)firstName);
  15. //......
  16. //所有这些应用都是要释放的,手册里是说“you are responsible to release it"
  17. (firstName==NULL)?:CFRelease(firstName);
  18. (lastName==NULL)?:CFRelease(lastName);
  19. (company==NULL)?:CFRelease(company);
  20. (department==NULL)?:CFRelease(department);
  21. (job==NULL)?:CFRelease(job);
  22. //.......
  23. //有些属性不是唯一的,比如一个人有多个电话:手机,主电话,传真。。。
  24. ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
  25. //所有ABMutableMultiValueRef这样的引用的东西都是这样一个元组(id,label,value)
  26. multiPhone = ABRecordCopyValue(record, kABPersonPhoneProperty);
  27. for (CFIndex i = 0; i < ABMultiValueGetCount(multiPhone); i++) {
  28. CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(multiPhone, i);
  29. CFStringRef numberRef = ABMultiValueCopyValueAtIndex(multiPhone, i);
  30. //可以通过元组的label来判定这个电话是哪种电话,比如下面就包括:主电话,手机,工作传真
  31. if([(NSString *)labelRef isEqualToString:(NSString *) kABPersonPhoneMainLabel]){
  32. person._mainPhone = (NSString *)numberRef;
  33. }else if([(NSString *)labelRef isEqualToString:(NSString *) kABPersonPhoneMobileLabel]){
  34. person._cellPhone = (NSString *)numberRef;
  35. }else if([(NSString *)labelRef compare:(NSString *) kABPersonPhoneWorkFAXLabel]==NSOrderedSame){
  36. person._fax = (NSString *)numberRef;
  37. }
  38. CFRelease(labelRef);
  39. CFRelease(numberRef);
  40. }
  41. CFRelease(multiPhone);
  42. }
  43. //释放资源
  44. //其他还有url,email,地址等等属性都是ABMutableMultiValueRef多值类型的,可以采用循环来遍历

addressbook中的好友名字安姓氏安索引显示

转自  http://blog.csdn.net/chyroger/archive/2010/08/10/5800842.aspx
  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. ABAddressBookRef addressBook = ABAddressBookCreate();
  4. CFArrayRef friendList = ABAddressBookCopyArrayOfAllPeople(addressBook);
  5. UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];//这个是建立索引的核心
  6. self._allFriends = [[NSMutableArray arrayWithCapacity:1] retain];//_allFriends 是一个NSMutableArray型的成员变量
  7. int friendsCount = CFArrayGetCount(friendList);
  8. NSMutableArray *temp = [NSMutableArray arrayWithCapacity:0];
  9. for (int i = 0; i<friendsCount; i++) {
  10. NameIndex *item = [[NameIndex alloc] init];//NameIndex是一个用于给UILocalizedIndexedCollation类对象做索引的类,代码见下个代码块
  11. ABRecordRef record = CFArrayGetValueAtIndex(friendList, i);
  12. CFStringRef firstName = ABRecordCopyValue(record, kABPersonFirstNameProperty);
  13. CFStringRef lastName =  ABRecordCopyValue(record, kABPersonLastNameProperty);
  14. item._lastName = (NSString*)lastName;
  15. item._firstName = (NSString*)firstName;
  16. item._originIndex = i;
  17. (lastName==NULL)?:CFRelease(lastName);
  18. (firstName==NULL)?:CFRelease(firstName);
  19. [temp addObject:item];
  20. [item release];
  21. }
  22. for (NameIndex *item in temp) {
  23. NSInteger sect = [theCollation sectionForObject:item collationStringSelector:@selector(getLastName)];//getLastName是实现中文安拼音检索的核心,见NameIndex类
  24. item._sectionNum = sect; //设定姓的索引编号
  25. }
  26. NSInteger highSection = [[theCollation sectionTitles] count]; //返回的应该是27,是a-z和#
  27. NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection]; //tableView 会被分成27个section
  28. for (int i=0; i<=highSection; i++) {
  29. NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
  30. [sectionArrays addObject:sectionArray];
  31. }
  32. for (NameIndex *item in temp) {
  33. [(NSMutableArray *)[sectionArrays objectAtIndex:item._sectionNum] addObject:item];
  34. }
  35. for (NSMutableArray *sectionArray in sectionArrays) {
  36. NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(getFirstName)]; //同
  37. [_allFriends addObject:sortedSection];
  38. }
  39. }

UiLocalizedIndexedCollation 类的需要一个返回为NSString的函数接口作为进行排序的字符串的输入,如果输入的是英文,自然没问题,如果是中文,那就会把中文都归到 “#”section下。如果我们能把中文对应的拼音字母传给这个- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector函数,那么就能实现排序了,原帖给的代码就实现了中文到拼音字母的转换。

上段代码中的NameIndex类如下

view plaincopy to clipboardprint?
  1. @interface NameIndex : NSObject {
  2. NSString *_lastName;
  3. NSString *_firstName;
  4. NSInteger _sectionNum;
  5. NSInteger _originIndex;
  6. }
  7. @property (nonatomic, retain) NSString *_lastName;
  8. @property (nonatomic, retain) NSString *_firstName;
  9. @property (nonatomic) NSInteger _sectionNum;
  10. @property (nonatomic) NSInteger _originIndex;
  11. - (NSString *) getFirstName;
  12. - (NSString *) getLastName;
  13. @end
  14. @implementation NameIndex
  15. @synthesize _firstName, _lastName;
  16. @synthesize _sectionNum, _originIndex;
  17. - (NSString *) getFirstName {
  18. if ([_firstName canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语
  19. return _firstName;
  20. }
  21. else { //如果是非英语
  22. return [NSString stringWithFormat:@"%c",pinyinFirstLetter([_firstName characterAtIndex:0])];
  23. }
  24. }
  25. - (NSString *) getLastName {
  26. if ([_lastName canBeConvertedToEncoding:NSASCIIStringEncoding]) {
  27. return _lastName;
  28. }
  29. else {
  30. return [NSString stringWithFormat:@"%c",pinyinFirstLetter([_lastName characterAtIndex:0])];
  31. }
  32. }
  33. - (void)dealloc {
  34. [_firstName release];
  35. [_lastName release];
  36. [super dealloc];
  37. }
  38. @end

剩 下的就是tableview的几个delegate方法了,更详尽的tableview实现Index可以参见官方文档Table View Programming Guide for iPhone OS的第41页,即第4章的Populating an IndexedList小节,我这里实现的代码如下:

view plaincopy to clipboardprint?
  1. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
  2. return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
  3. }
  4. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  5. if ([[_allFriends objectAtIndex:section] count] > 0) {
  6. return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
  7. }
  8. return nil;
  9. }
  10. - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
  11. {
  12. return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
  13. }
  14. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  15. return [_allFriends count];
  16. }
  17. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  18. return [(NSArray*)[_allFriends objectAtIndex:section] count];
  19. }
  20. // Customize the appearance of table view cells.
  21. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  22. static NSString *CellIdentifier = @"Cell";
  23. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  24. if (cell == nil) {
  25. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
  26. }
  27. // Set up the cell...
  28. cell.textLabel.text = [NSString stringWithFormat:@"%@%@",((NameIndex*)[[_allFriends objectAtIndex:indexPath.section] objectAtIndex:indexPath.row])._lastName ,((NameIndex*)[[_allFriends objectAtIndex:indexPath.section] objectAtIndex:indexPath.row])._firstName];
  29. return cell;
  30. }

转载于:https://www.cnblogs.com/codeApp/archive/2012/11/16/2773697.html

iphone addressbook操作相关推荐

  1. iphone的操作系统介绍_操作系统介绍

    iphone的操作系统介绍 A computer system has many resources (hardware and software), which may be require to ...

  2. 苹果待处理订单要多久_用苹果 iPhone 时操作失误被扣费,该如何申请退款?

    不知道大家是否遇到过这种情况: 在 iPhone 上使用某些应用时,显示需要付费,自己本身是拒绝的,结果一不小心点错了,出现误操作导致出现了扣费的问题. 咳咳,肯定不止我一个人遇到过,这时候很多人就慌 ...

  3. 计算机怎么操作文档,iPhone如何操作电脑文件 文件共享功能了解一下

    iPhone的iOS系统中有不少便携的小功能,他们隐藏的比较深,身边不少果粉都不清楚有这些功能,例如:文件共享.这个功能可以让你在iPhone手机上访问电脑上的文件,只需两者之间在同一局域网下,就可以 ...

  4. iphone 文件操作以及文件管理

    iPhone开发中文件读写教程是本文要介绍的内容,主要是来学习iphone开发中关于文件的操作,具体内容来看本文详细讲解.对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(所谓sa ...

  5. 瑞讯银行远程视频开户安卓及苹果iphone手机操作

    第一步 :填写在线申请表 首先,请到我们网站上在线填写开户申请表: https://apply.swissquote.com/fx/?lang=cn 以下几点需要注意: 1,开户表格可以用中文填写,因 ...

  6. 苹果手机计算机的使用技巧,IPHONE玩机技巧介绍 让你的操作更高效

    苹果手机除了对硬件方面有精心的设计,对于用户的操作方面同样也花了不少心思,别看平时我们用的多,也许有许多操作是你平时根本就不知道的,而一旦熟悉了这种操作,你的操作效率将会大大提升,那么都有哪些IPHO ...

  7. UIImagePickerController在iPhone和iPad中用法的一点不同[转]

    我们知道,在iPhone中获取照片库常用的方法如下: UIImagePickerController *m_imagePicker = [[UIImagePickerController alloc] ...

  8. exxi6.7如何传文件到win7_比QQ直传快100倍!它让PC、安卓、iPhone光速互传文件

    想了解有趣有料的数码资讯,手机深度评测,数码选购要点,最新薅羊毛秘诀,动动手指,点击关注我们.避坑的事交给我们来做吧- 每个星期上班都是个煎熬,各种意外在等着你,比如平常工作中各种图片.文章.数据等等 ...

  9. android itool 备份,教您如何将Android SMS导入iPhone

    [摘要] iPhone6即将发布,许多Android用户将希望更改为iPhone6,但是当前的各种软件无法发送Android SMS. -> iPhone6即将面世,许多Android用户将希望 ...

最新文章

  1. C# 中Excel导出,可以自由设置导出的excel格式
  2. 阿里云容器服务多项重磅发布:高效智能、安全无界的新一代平台
  3. 4.4 一个完整的Google Maps应用
  4. python课后题答案第二章_Python编程:从入门到实践——练习题答案(第二章)
  5. eclipse 如何关联git_作为一名初学Java者 如何做简单的Java项目
  6. NEW:SlickEdit Pro 27.0 KEY
  7. Matlab计算机视觉/图像处理工具箱推荐
  8. web前端面试题讲解-网站重构的理解
  9. mapgis 转换为CAD格式图形 显示不出来的处理
  10. “单向网闸”技术介绍-网络隔离的新型产品
  11. 电商平台是如何赚钱的?
  12. 好听的名字 - 收藏给宝宝起名字用
  13. 知识兔Excel教程:让同事看傻~这几个Excel技巧太牛了
  14. 如果评估销售奖金设计的有效性?
  15. [转]Go在谷歌:以软件工程为目的的语言设计
  16. Java设计模式-模板方法模式详解
  17. 计算机网络局域网仿真搭建,基于虚拟仿真技术的计算机网络实验室的构建.pdf...
  18. 位置环与速度环的串级PID
  19. Android模拟器系统应用卸载办法
  20. 兼容cc1101/cmt2300-DP4301 SUB-1G 无线收发芯片

热门文章

  1. 2012年3月51CTO壁纸点评活动获奖名单【已结束】
  2. msf win10漏洞_【CVE20200796】漏洞复现
  3. RAID阵列及常用RAID
  4. 10虚拟机的删除和迁移
  5. 3-14pytorch与统计学方法
  6. STM32板子电源绘制原理
  7. python爬取天天基金网_天天基金网精选基金组合年度报告20201231
  8. 为什么虚拟机上一运行就显示程序停止_五分钟学Java:如何学习Java面试必考的JVM虚拟机||CSDN博文精选...
  9. 计算机专业很不稳定,浅谈计算机网络专业教学的创新因素:引起计算机系统不稳定的因素有哪些...
  10. php 数组xml,php 数组转xml的例子