ios 简书 获取通讯录信息_ios 获取本地通讯录信息
iphone如許app讀取通訊錄信息,讀取通訊錄信息時需要加載AddressBookUI 和AddressBook兩個包,具體實現(xiàn)方法如下
ABAddressBookRef addressBook = ABAddressBookCreate();//定義通訊錄名字為addressbook
CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);//將通訊錄中的信息用數(shù)組方式讀出
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);//獲取通訊錄中聯(lián)系人
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);//讀取通訊錄中聯(lián)系人的唯一標識
NSDate * createDate = (NSDate *)ABRecordCopyValue(person, kABPersonCreationDateProperty);// 讀取通訊錄中聯(lián)系人的創(chuàng)建日期
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];
//上訴操作是將某個聯(lián)系人的標識號與創(chuàng)建日期進行組合,得到唯一的標識,是為了當時特殊的需要,一般不會有這種變態(tài)應用,這是因為ABRecordGetRecordID在一個手機中是唯一的,即使刪掉某一個聯(lián)系人,這個號也不會在被占用。
//讀取聯(lián)系人姓名屬性
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];
}
//讀取聯(lián)系人姓名的第一個漢語拼音,用于排序,調(diào)用此方法需要在程序中加載pingyin.h pingyin.c,如有需要請給我聯(lián)系。
iphoneContact.pinyin =[[NSString stringWithFormat:@"%c",pinyinFirstLetter([iphoneContact.contactName characterAtIndex:0])] uppercaseString];
//讀取聯(lián)系人公司信息
iphoneContact.contactCompany = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
//讀取聯(lián)系人工作
iphoneContact.contactJob = (NSString *)ABRecordCopyValue(person, kABPersonJobTitleProperty);
//讀取聯(lián)系人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:@"_$!!$_"]) {
iphoneContact.contactEmail = aEmail;
}
}
}
//讀取通信地址信息,在ios中國家,省份,城市,區(qū),街道,號都是單獨存儲的,需要單獨讀取然后在組合(根據(jù)需要)
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:@"_$!!$_"]) {
iphoneContact.contactMobile= aPhone;
}
if ([aLabel isEqualToString:@"_$!!$_"]) {
iphoneContact.contactFax = aPhone;
}
if ([aLabel isEqualToString:@"_$!!$_"]) {
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();//上訴代碼實現(xiàn)圖片壓縮,可根據(jù)需要選擇,現(xiàn)在是壓縮到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];//將讀取的某一個人信息放到我們自定義的可變數(shù)組中
}
總結(jié)
以上是生活随笔為你收集整理的ios 简书 获取通讯录信息_ios 获取本地通讯录信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL server 2008数据库的备
- 下一篇: 人人都有极客精神