日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ios 简书 获取通讯录信息_ios 获取本地通讯录信息

發(fā)布時間:2023/12/10 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios 简书 获取通讯录信息_ios 获取本地通讯录信息 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

iphone如許app讀取通訊錄信息,讀取通訊錄信息時需要加載AddressBookUI 和AddressBook兩個包,具體實現(xiàn)方法如下

ABAddressBookRef addressBook = ABAddressBookCreate();//定義通訊錄名字為addressbook

CFArrayRef contacts = ABAddressBookCopyArrayOfAllPeople(addressBook);//將通訊錄中的信息用數組方式讀出

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)系人姓名的第一個漢語拼音,用于排序,調用此方法需要在程序中加載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ū),街道,號都是單獨存儲的,需要單獨讀取然后在組合(根據需要)

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)圖片壓縮,可根據需要選擇,現(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];//將讀取的某一個人信息放到我們自定義的可變數組中

}

總結

以上是生活随笔為你收集整理的ios 简书 获取通讯录信息_ios 获取本地通讯录信息的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。