iphone-common-codes-ccteam源代码 CCAddressBook.m
程序员文章站
2024-01-15 18:03:52
...
//
// 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链接地址(会有更新):[url]http://code.google.com/p/iphone-common-codes-ccteam/source/browse/trunk/CCFC/files/CCAddressBook.m[/url]
上一篇: ps 制作青色的橄榄
推荐阅读
-
iphone-common-codes-ccteam源代码 CCAddressBook.m
-
iphone-common-codes-ccteam源代码 CCDylib.h
-
c语言stdio.h源代码
-
iphone-common-codes-ccteam源代码 CCDebug.h
-
iphone-common-codes-ccteam源代码 CCDepend.m
-
javascript - 【PHP】【.NET】【JS】【AJAX】关于抓取网页源代码的问题
-
如何编译PHP源代码介绍ZendEncode的安装方法我今天刚调试成功,不敢独享,拿来献宝_PHP教程
-
简单计数器的php源代码
-
对页面的源代码进行加密,使源代码变成乱码,没法读取_PHP教程
-
Android仿QQ未读消息--红点拖拽删除【源代码】