iphone addressbook操作

    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.       
  23.     for (NameIndex *item in temp) {   
  24.         NSInteger sect = [theCollation sectionForObject:item collationStringSelector:@selector(getLastName)];//getLastName是实现中文安拼音检索的核心,见NameIndex类   
  25.         item._sectionNum = sect; //设定姓的索引编号  
  26.     }   
  27.     NSInteger highSection = [[theCollation sectionTitles] count]; //返回的应该是27,是a-z和#  
  28.     NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection]; //tableView 会被分成27个section  
  29.     for (int i=0; i<=highSection; i++) {   
  30.         NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];   
  31.         [sectionArrays addObject:sectionArray];   
  32.     }   
  33.     for (NameIndex *item in temp) {   
  34.         [(NSMutableArray *)[sectionArrays objectAtIndex:item._sectionNum] addObject:item];   
  35.     }   
  36.     for (NSMutableArray *sectionArray in sectionArrays) {   
  37.         NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(getFirstName)]; //同  
  38.         [_allFriends addObject:sortedSection];   
  39.     }   
  40. }  

 

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

上段代码中的NameIndex类如下

  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.   
  18. - (NSString *) getFirstName {  
  19.     if ([_firstName canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语  
  20.         return _firstName;  
  21.     }  
  22.     else { //如果是非英语  
  23.         return [NSString stringWithFormat:@"%c",pinyinFirstLetter([_firstName characterAtIndex:0])];  
  24.     }  
  25.       
  26. }  
  27. - (NSString *) getLastName {  
  28.     if ([_lastName canBeConvertedToEncoding:NSASCIIStringEncoding]) {  
  29.         return _lastName;  
  30.     }  
  31.     else {  
  32.         return [NSString stringWithFormat:@"%c",pinyinFirstLetter([_lastName characterAtIndex:0])];  
  33.     }  
  34.       
  35. }  
  36. - (void)dealloc {  
  37.     [_firstName release];  
  38.     [_lastName release];  
  39.     [super dealloc];  
  40. }  
  41. @end  

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

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

 

posted on 2012-11-16 17:42  无量少年  阅读(340)  评论(0编辑  收藏  举报

导航