iphone-common-codes-ccteam源代码 CCAddressBook.m

View Code
//  
// CCAddressBook.m
// CCFC
//
// Created by xichen on 11-12-16.
// Copyright 2011年 ccteam. All rights reserved.
//


#import "CCAddressBook.h"


@implementation CCAddressBook


- (id)init
{
self = [super init];
if(self)
{
ABAddressBookRef addressBook = ABAddressBookCreate();
_abAllPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
_abGroups = (NSArray *)ABAddressBookCopyArrayOfAllGroups(addressBook);
}
return self;
}




- (void)dealloc
{
[_abAllPeople release];
[_abGroups release];
[super dealloc];
}


- (int)getABRecordCount
{
return [_abAllPeople count];
}


- (int)getABGroupCount
{
return [_abGroups count];
}




- (ABRecordRef)getABRecordRefByIndex:(int)index
{
return [_abAllPeople objectAtIndex:index];
}


- (ABRecordID)getABRecordIDByIndex:(int)index
{
return ABRecordGetRecordID([self getABRecordRefByIndex:index]);
}


// 获得通讯录联系人的全名
// 和getFullNameByRecord略有不同,例如,如果通讯录的姓保存的是"陈",名保存的是"曦",那么此得到的
// 是 "陈 曦", getFullNameByRecord得到的是"陈曦"
- (NSString *)getCompositeNameByIndex:(int)index
{
return [(NSString *)ABRecordCopyCompositeName([self getABRecordRefByIndex:index]) autorelease];
}


- (NSMutableArray *)getPhoneNumberArrByIndex:(int)index
{
ABRecordRef ref = [self getABRecordRefByIndex:index];
// 获取号码数组
ABMultiValueRef tempArr = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
if(!tempArr)
{
return nil;
}

NSMutableArray *phoneArr = [NSMutableArray new];
for(int i = 0; i < ABMultiValueGetCount(tempArr); ++i)
{
NSString *phoneNo = (NSString *)ABMultiValueCopyValueAtIndex(tempArr, i);
[phoneArr addObject:phoneNo]; // 依次将号码加入新数组中
CFRelease(phoneNo);
}

CFRelease(tempArr);
return [phoneArr autorelease];
}


// 根据ABRecordRef获取联系人姓名全称
- (NSString *)getFullNameByRecord:(ABRecordRef)record
{
NSString *firstName = (NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
NSString *midName = (NSString *)ABRecordCopyValue(record, kABPersonMiddleNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);


if(firstName == nil)
firstName = @"";
if(midName == nil)
midName = @"";
if(lastName == nil)
lastName = @"";

CFRelease(firstName);
CFRelease(lastName);
NSArray *languages = [NSLocale preferredLanguages];
NSString *langName = [languages objectAtIndex:0];
// 不需要使用 MiddleName
if([langName isEqualToString:@"en"])
return [NSString stringWithFormat:@"%@ %@", firstName, lastName];
else if([langName isEqualToString:@"zh-Hans"])
return [NSString stringWithFormat:@"%@%@", lastName, firstName];
else
return [NSString stringWithFormat:@"%@%@%@", firstName, midName, lastName];
}


// 根据index获取联系人姓名全称
- (NSString *)getFullNameByIndex:(int)index
{
ABRecordRef ref = [self getABRecordRefByIndex:index];
return [self getFullNameByRecord:ref];
}




@end

 

googlecode链接地址(会有更新):http://code.google.com/p/iphone-common-codes-ccteam/source/browse/trunk/CCFC/files/CCAddressBook.m

posted @ 2011-12-30 10:53  cc_team  阅读(430)  评论(2编辑  收藏  举报