這個 資料有些老了 。部分東西還時能夠湊合用。html
原文出自 http://blog.sina.com.cn/s/blog_68eb52b301013x7n.htmlios
iphone如許app讀取通信錄信息,讀取通信錄信息時須要加載AddressBookUI 和AddressBook兩個包,具體實現方法以下
ABAddressBookRef addressBook = ABAddressBookCreate();//定義通信錄名字爲addressbook
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);//將通信錄中的信息用數組方式讀出
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);//獲取通信錄中聯繫人
iphoneContactList = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < nPeople; i++)
{
IphoneContact * iphoneContact = [[IphoneContact alloc] init];
NSData *imageData = [[[NSData alloc] init]autorelease];
NSString *address = [[[NSString alloc] init]autorelease];
ABRecordRef person = CFArrayGetValueAtIndex(contacts, i);//取出某一我的的信息
NSInteger lookupforkey =(NSInteger)ABRecordGetRecordID(person);//讀取通信錄中聯繫人的惟一標識
NSDate * createDate = (NSDate *)ABRecordCopyValue(person, kABPersonCreationDateProperty);// 讀取通信錄中聯繫人的建立日期
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString* birthDay = [formatter stringFromDate:createDate];
[formatter release];
NSString *createDate1 = [birthDay stringByReplacingOccurrencesOfString:@"-" withString:@""];
NSString *createDate2 = [createDate1 stringByReplacingOccurrencesOfString:@":" withString:@""];
NSString *createDate3 = [createDate2 stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"1111========%@",createDate3);
NSLog(@"222222========%@",birthDay);
NSString *indexInIphone = [NSString stringWithFormat:@"%i",lookupforkey];
iphoneContact.lookUpKey = [createDate3 stringByAppendingString:indexInIphone];
//上訴操做是將某個聯繫人的標識號與建立日期進行組合,獲得惟一的標識,是爲了當時特殊的須要,通常不會有這種變態應用,這是由於ABRecordGetRecordID在一個手機中是惟一的,即便刪掉某一個聯繫人,這個號也不會在被佔用。
//讀取聯繫人姓名屬性
if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))== nil) {
iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
}else if (ABRecordCopyValue(person, kABPersonLastNameProperty) == nil&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))){
iphoneContact.contactName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
}else if (ABRecordCopyValue(person, kABPersonLastNameProperty)&&(ABRecordCopyValue(person, kABPersonFirstNameProperty))){
NSString *first =(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *last = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
iphoneContact.contactName = [NSString stringWithFormat:@"%@%@",last,first];
}
//讀取聯繫人姓名的第一個漢語拼音,用於排序,調用此方法須要在程序中加載pingyin.h pingyin.c,若有須要請給我聯繫。
iphoneContact.pinyin =[[NSString stringWithFormat:@"%c",pinyinFirstLetter([iphoneContact.contactName characterAtIndex:0])] uppercaseString];
//讀取聯繫人公司信息
iphoneContact.contactCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
//讀取聯繫人工做
iphoneContact.contactJob = (NSString *)ABRecordCopyValue(person, kABPersonJobTitleProperty);
//讀取聯繫人email信息,email有好幾種,好比工做郵箱,家庭郵箱,經過ABMultiValueCopyLabelAtIndex 屬性來判斷,下面給出了例子
ABMultiValueRef emailForWORK = ABRecordCopyValue(person, kABPersonEmailProperty);
if ((emailForWORK != nil)&&ABMultiValueGetCount(emailForWORK)>0) {
for (int k = 0; k < ABMultiValueGetCount(emailForWORK); k++) {
NSString * aEmail = (NSString *)ABMultiValueCopyValueAtIndex(emailForWORK, k);
NSString * aEmailLabel = (NSString *)ABMultiValueCopyLabelAtIndex(emailForWORK, k);
if ([aEmailLabel isEqualToString:@"_$!<Work>!$_"]) {
iphoneContact.contactEmail = aEmail;
}
}
}
//讀取通訊地址信息,在ios中國家,省份,城市,區,街道,號都是單獨存儲的,須要單獨讀取而後在組合(根據須要)
ABMultiValueRef addressTotal = ABRecordCopyValue(person, kABPersonAddressProperty);
if (addressTotal) {
int count = ABMultiValueGetCount(addressTotal);
for(int j = 0; j < count; j++)
{
NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(addressTotal, j);
NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
if(country != nil)
address = [NSString stringWithFormat:@"%@",country];
NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
if(city != nil)
address = [address stringByAppendingFormat:@"%@",city];
NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
if(state != nil)
address = [address stringByAppendingFormat:@"%@",state];
NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
if(street != nil)
address = [address stringByAppendingFormat:@"%@",street];
}
iphoneContact.contactAdress = address;
}
//讀取電話信息,和emial相似,也分爲工做電話,家庭電話,工做傳真,家庭傳真。。。。
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
if ((phone != nil)&&ABMultiValueGetCount(phone)>0) {
for (int m = 0; m < ABMultiValueGetCount(phone); m++) {
NSString * aPhone = [(NSString *)ABMultiValueCopyValueAtIndex(phone, m) autorelease];
NSString * aLabel = [(NSString *)ABMultiValueCopyLabelAtIndex(phone, m) autorelease];
if ([aLabel isEqualToString:@"_$!<Mobile>!$_"]) {
iphoneContact.contactMobile= aPhone;
}
if ([aLabel isEqualToString:@"_$!<WorkFAX>!$_"]) {
iphoneContact.contactFax = aPhone;
}
if ([aLabel isEqualToString:@"_$!<Work>!$_"]) {
iphoneContact.contactTelephone= aPhone;
}
}
}
//讀取照片信息
imageData = (NSData *)ABPersonCopyImageData(person);
UIImage *myImage = [UIImage imageWithData:imageData];
CGSize newSize = CGSizeMake(55, 55);
UIGraphicsBeginImageContext(newSize);
[myImage drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();//上訴代碼實現圖片壓縮,可根據須要選擇,如今是壓縮到55*55
imageData = UIImagePNGRepresentation(newImage);
if (imageData) {
NSData * BimageData = [GTMBase64 encodeData:imageData];
iphoneContact.headImage= [[NSString alloc] initWithData:BimageData encoding:NSUTF8StringEncoding];
}
iphoneContact.isSelected = NO;
[iphoneContactList addObject:iphoneContact]; //將讀取的某一我的信息放到咱們自定義的可變數組中
}數組