iphone addressbook操作
- //get all people info from the address book
- ABAddressBookRef addressBook = ABAddressBookCreate();
- CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);//这是个数组的引用
- for(int i = 0; i<CFArrayGetCount(people); i++){
- //parse each person of addressbook
- ABRecordRef record=CFArrayGetValueAtIndex(people, i);//取出一条记录
- //以下的属性都是唯一的,即一个人只有一个FirstName,一个Organization。。。
- CFStringRef firstName = ABRecordCopyValue(record,kABPersonFirstNameProperty);
- CFStringRef lastName = ABRecordCopyValue(record,kABPersonLastNameProperty);
- CFStringRef company = ABRecordCopyValue(record,kABPersonOrganizationProperty);
- CFStringRef department = ABRecordCopyValue(record,kABPersonDepartmentProperty);
- CFStringRef job = ABRecordCopyValue(record,kABPersonJobTitleProperty);
- //"CFStringRef"这个类型也是个引用,可以转成NSString*
- NSlog((NSString *)firstName);
- //......
- //所有这些应用都是要释放的,手册里是说“you are responsible to release it"
- (firstName==NULL)?:CFRelease(firstName);
- (lastName==NULL)?:CFRelease(lastName);
- (company==NULL)?:CFRelease(company);
- (department==NULL)?:CFRelease(department);
- (job==NULL)?:CFRelease(job);
- //.......
- //有些属性不是唯一的,比如一个人有多个电话:手机,主电话,传真。。。
- ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
- //所有ABMutableMultiValueRef这样的引用的东西都是这样一个元组(id,label,value)
- multiPhone = ABRecordCopyValue(record, kABPersonPhoneProperty);
- for (CFIndex i = 0; i < ABMultiValueGetCount(multiPhone); i++) {
- CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(multiPhone, i);
- CFStringRef numberRef = ABMultiValueCopyValueAtIndex(multiPhone, i);
- //可以通过元组的label来判定这个电话是哪种电话,比如下面就包括:主电话,手机,工作传真
- if([(NSString *)labelRef isEqualToString:(NSString *) kABPersonPhoneMainLabel]){
- person._mainPhone = (NSString *)numberRef;
- }else if([(NSString *)labelRef isEqualToString:(NSString *) kABPersonPhoneMobileLabel]){
- person._cellPhone = (NSString *)numberRef;
- }else if([(NSString *)labelRef compare:(NSString *) kABPersonPhoneWorkFAXLabel]==NSOrderedSame){
- person._fax = (NSString *)numberRef;
- }
- CFRelease(labelRef);
- CFRelease(numberRef);
- }
- CFRelease(multiPhone);
- }
- //释放资源
- //其他还有url,email,地址等等属性都是ABMutableMultiValueRef多值类型的,可以采用循环来遍历
addressbook中的好友名字安姓氏安索引显示
转自 http://blog.csdn.net/chyroger/archive/2010/08/10/5800842.aspx
- - (void)viewDidLoad {
- [super viewDidLoad];
- ABAddressBookRef addressBook = ABAddressBookCreate();
- CFArrayRef friendList = ABAddressBookCopyArrayOfAllPeople(addressBook);
- UILocalizedIndexedCollation *theCollation = [UILocalizedIndexedCollation currentCollation];//这个是建立索引的核心
- self._allFriends = [[NSMutableArray arrayWithCapacity:1] retain];//_allFriends 是一个NSMutableArray型的成员变量
- int friendsCount = CFArrayGetCount(friendList);
- NSMutableArray *temp = [NSMutableArray arrayWithCapacity:0];
- for (int i = 0; i<friendsCount; i++) {
- NameIndex *item = [[NameIndex alloc] init];//NameIndex是一个用于给UILocalizedIndexedCollation类对象做索引的类,代码见下个代码块
- ABRecordRef record = CFArrayGetValueAtIndex(friendList, i);
- CFStringRef firstName = ABRecordCopyValue(record, kABPersonFirstNameProperty);
- CFStringRef lastName = ABRecordCopyValue(record, kABPersonLastNameProperty);
- item._lastName = (NSString*)lastName;
- item._firstName = (NSString*)firstName;
- item._originIndex = i;
- (lastName==NULL)?:CFRelease(lastName);
- (firstName==NULL)?:CFRelease(firstName);
- [temp addObject:item];
- [item release];
- }
- for (NameIndex *item in temp) {
- NSInteger sect = [theCollation sectionForObject:item collationStringSelector:@selector(getLastName)];//getLastName是实现中文安拼音检索的核心,见NameIndex类
- item._sectionNum = sect; //设定姓的索引编号
- }
- NSInteger highSection = [[theCollation sectionTitles] count]; //返回的应该是27,是a-z和#
- NSMutableArray *sectionArrays = [NSMutableArray arrayWithCapacity:highSection]; //tableView 会被分成27个section
- for (int i=0; i<=highSection; i++) {
- NSMutableArray *sectionArray = [NSMutableArray arrayWithCapacity:1];
- [sectionArrays addObject:sectionArray];
- }
- for (NameIndex *item in temp) {
- [(NSMutableArray *)[sectionArrays objectAtIndex:item._sectionNum] addObject:item];
- }
- for (NSMutableArray *sectionArray in sectionArrays) {
- NSArray *sortedSection = [theCollation sortedArrayFromArray:sectionArray collationStringSelector:@selector(getFirstName)]; //同
- [_allFriends addObject:sortedSection];
- }
- }
UiLocalizedIndexedCollation 类的需要一个返回为NSString的函数接口作为进行排序的字符串的输入,如果输入的是英文,自然没问题,如果是中文,那就会把中文都归到 “#”section下。如果我们能把中文对应的拼音字母传给这个- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector函数,那么就能实现排序了,原帖给的代码就实现了中文到拼音字母的转换。
上段代码中的NameIndex类如下
- @interface NameIndex : NSObject {
- NSString *_lastName;
- NSString *_firstName;
- NSInteger _sectionNum;
- NSInteger _originIndex;
- }
- @property (nonatomic, retain) NSString *_lastName;
- @property (nonatomic, retain) NSString *_firstName;
- @property (nonatomic) NSInteger _sectionNum;
- @property (nonatomic) NSInteger _originIndex;
- - (NSString *) getFirstName;
- - (NSString *) getLastName;
- @end
- @implementation NameIndex
- @synthesize _firstName, _lastName;
- @synthesize _sectionNum, _originIndex;
- - (NSString *) getFirstName {
- if ([_firstName canBeConvertedToEncoding: NSASCIIStringEncoding]) {//如果是英语
- return _firstName;
- }
- else { //如果是非英语
- return [NSString stringWithFormat:@"%c",pinyinFirstLetter([_firstName characterAtIndex:0])];
- }
- }
- - (NSString *) getLastName {
- if ([_lastName canBeConvertedToEncoding:NSASCIIStringEncoding]) {
- return _lastName;
- }
- else {
- return [NSString stringWithFormat:@"%c",pinyinFirstLetter([_lastName characterAtIndex:0])];
- }
- }
- - (void)dealloc {
- [_firstName release];
- [_lastName release];
- [super dealloc];
- }
- @end
剩 下的就是tableview的几个delegate方法了,更详尽的tableview实现Index可以参见官方文档Table View Programming Guide for iPhone OS的第41页,即第4章的Populating an IndexedList小节,我这里实现的代码如下:
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
- return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
- }
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
- if ([[_allFriends objectAtIndex:section] count] > 0) {
- return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
- }
- return nil;
- }
- - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
- {
- return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
- }
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return [_allFriends count];
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [(NSArray*)[_allFriends objectAtIndex:section] count];
- }
- // Customize the appearance of table view cells.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- }
- // Set up the cell...
- cell.textLabel.text = [NSString stringWithFormat:@"%@%@",((NameIndex*)[[_allFriends objectAtIndex:indexPath.section] objectAtIndex:indexPath.row])._lastName ,((NameIndex*)[[_allFriends objectAtIndex:indexPath.section] objectAtIndex:indexPath.row])._firstName];
- return cell;
- }