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. 20151020sql2
  2. 人工智能三年行动计划启动,推动人工智能和实体经济深度融合
  3. [swustoj 856] Huge Tree
  4. 在AD中恢复误删除的对象
  5. 计算机二级考数组吗,计算机二级考试!
  6. 互联网java常用框架_来,带你鸟瞰 Java 中4款常用的并发框架!
  7. 直播预告 - 博时基金DevOps体系建设和自动化测试分享
  8. [Swift]LeetCode884. 两句话中的不常见单词 | Uncommon Words from Two Sentences
  9. Azure Site Recovery之启用复制
  10. win8.1 安装.NET Framework3.5
  11. 来几个 9 块 9 好吃零食
  12. 宣传失真,二手人生。
  13. VO、DTO、BO、PO、DO、POJO 数据模型的理解和实际使用
  14. AAAI-2021-RE-Progressive Multitask Learning with Controlled Information Flow for Joint Entity and Re
  15. 三步棋,跟着bit鹏哥学得
  16. 密码学的安全性浅析-3
  17. “自动修复”无法修复你的电脑-SATAFIRM S11-固态硬盘坏了
  18. uniapp vue 微信小程序 前端 直传华为云对象存储OBS
  19. 大数据学习一般都需要学习哪些知识
  20. JAVA数据加密压缩传输给服务端(Gzip加AES)

热门文章

  1. linux rhel5.6 使用163yum源
  2. VC++设计简易计算器笔记(一)
  3. SQL Server 2005中专用管理员连接 (DAC) 使用技巧修改系统表的方法
  4. 电脑护眼设置_解锁办公新技能 海信护眼平板Q5玩转工作无负担-科技频道
  5. 负压电路_通风设备之负压风机的工作原理与安装方法是怎样的?
  6. c python boost.python_如何利用Boost.Python实现Python C/C++混合编程详解
  7. tum数据集_数值预报尚能战否?来自数据驱动的挑战
  8. Python机器学习:SVM005SVM使用多项式特征
  9. 华为全面屏鸿蒙,华为5G概念新机:真全面屏+鸿蒙OS 这才是旗舰手机
  10. java word转html 空指针_Windows 内核漏洞学习—空指针解引用