您好,登錄后才能下訂單哦!
這篇文章主要介紹了ios如何實現(xiàn)通訊錄聯(lián)系人各屬性獲取,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);
for(int i = 0; i < CFArrayGetCount(results); i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(results, i);
//讀取firstname
NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
if(personName != nil)
label.text = [label.text stringByAppendingFormat:@"姓名:%@",personName];
//讀取lastname
NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
if(lastname != nil)
label.text = [label.text stringByAppendingFormat:@"%@",lastname];
//讀取middlename
NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);
if(middlename != nil)
label.text = [label.text stringByAppendingFormat:@"%@",middlename];
//讀取prefix前綴
NSString *prefix = (NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);
if(prefix != nil)
label.text = [label.text stringByAppendingFormat:@"%@",prefix];
//讀取suffix后綴
NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);
if(suffix != nil)
label.text = [label.text stringByAppendingFormat:@"%@",suffix];
//讀取nickname呢稱
NSString *nickname = (NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);
if(nickname != nil)
label.text = [label.text stringByAppendingFormat:@"%@",nickname];
//讀取firstname拼音音標(biāo)
NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person,kABPersonFirstNamePhoneticProperty);
if(firstnamePhonetic != nil)
label.text = [label.text stringByAppendingFormat:@"%@",firstnamePhonetic];
//讀取lastname拼音音標(biāo)
NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person,kABPersonLastNamePhoneticProperty);
if(lastnamePhonetic != nil)
label.text = [label.text stringByAppendingFormat:@"%@",lastnamePhonetic];
//讀取middlename拼音音標(biāo)
NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person,kABPersonMiddleNamePhoneticProperty);
if(middlenamePhonetic != nil)
label.text = [label.text stringByAppendingFormat:@"%@",middlenamePhonetic];
//讀取organization公司
NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
if(organization != nil)
label.text = [label.text stringByAppendingFormat:@"%@",organization];
//讀取jobtitle工作
NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);
if(jobtitle != nil)
label.text = [label.text stringByAppendingFormat:@"%@",jobtitle];
//讀取department部門
NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);
if(department != nil)
label.text = [label.text stringByAppendingFormat:@"%@",department];
//讀取birthday生日
NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);
if(birthday != nil)
label.text = [label.text stringByAppendingFormat:@"%@",birthday];
//讀取note備忘錄
NSString *note = (NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);
if(note != nil)
label.text = [label.text stringByAppendingFormat:@"%@",note];
//第一次添加該條記錄的時間
NSString *firstknow = (NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);
NSLog(@"第一次添加該條記錄的時間%@\n",firstknow);
//最后一次修改該條記錄的時間
NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);
NSLog(@"最后一次修改該條記錄的時間%@\n",lastknow);
//獲取email多值
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
int emailcount = ABMultiValueGetCount(email);
for (int x = 0; x < emailcount; x++)
{
//獲取email Label
NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));
//獲取email值
NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x);
label.text = [label.text stringByAppendingFormat:@"%@:%@",emailLabel,emailContent];
}
//讀取地址多值
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
int count = ABMultiValueGetCount(address);
for(int j = 0; j < count; j++)
{
//獲取地址Label
NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);
label.text = [label.text stringByAppendingFormat:@"%@",addressLabel];
//獲取該label下的地址6屬性
NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);
NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
if(country != nil)
label.text = [label.text stringByAppendingFormat:@"國家:%@",country];
NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
if(city != nil)
label.text = [label.text stringByAppendingFormat:@"城市:%@",city];
NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
if(state != nil)
label.text = [label.text stringByAppendingFormat:@"?。?@",state];
NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
if(street != nil)
label.text = [label.text stringByAppendingFormat:@"街道:%@",street];
NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];
if(zip != nil)
label.text = [label.text stringByAppendingFormat:@"郵編:%@",zip];
NSString* coutntrycode = [personaddress valueForKey:(NSString*)kABPersonAddressCountryCodeKey];
if(coutntrycode != nil)
label.text = [label.text stringByAppendingFormat:@"國家編號:%@",coutntrycode];
}
//獲取dates多值
ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);
int datescount = ABMultiValueGetCount(dates);
for (int y = 0; y < datescount; y++)
{
//獲取dates Label
NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));
//獲取dates值
NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y);
label.text = [label.text stringByAppendingFormat:@"%@:%@",datesLabel,datesContent];
}
//獲取kind值
CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);
if (recordType == kABPersonKindOrganization) {
// it's a company
NSLog(@"it's a company");
} else {
// it's a person, resource, or room
NSLog(@"it's a person, resource, or room");
}
//獲取IM多值
ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);
for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++)
{
//獲取IM Label
NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);
label.text = [label.text stringByAppendingFormat:@"%@",instantMessageLabel];
//獲取該label下的2屬性
NSDictionary* instantMessageContent =(NSDictionary*)ABMultiValueCopyValueAtIndex(instantMessage, l);
NSString* username = [instantMessageContent valueForKey:(NSString*)kABPersonInstantMessageUsernameKey];
if(username != nil)
label.text = [label.text stringByAppendingFormat:@"username:%@",username];
NSString* service = [instantMessageContent valueForKey:(NSString*)kABPersonInstantMessageServiceKey];
if(service != nil)
label.text = [label.text stringByAppendingFormat:@"service:%@",service];
}
//讀取電話多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int k = 0; k<ABMultiValueGetCount(phone); k++)
{
//獲取電話Label
NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));
//獲取該Label下的電話值
NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k);
label.text = [label.text stringByAppendingFormat:@"%@:%@",personPhoneLabel,personPhone];
}
//獲取URL多值
ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);
for (int m = 0; m < ABMultiValueGetCount(url); m++)
{
//獲取電話Label
NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));
//獲取該Label下的電話值
NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,m);
label.text = [label.text stringByAppendingFormat:@"%@:%@",urlLabel,urlContent];
}
//讀取照片
NSData *p_w_picpath = (NSData*)ABPersonCopyImageData(person);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)];
[myImage setImage:[UIImage p_w_picpathWithData:p_w_picpath]];
myImage.opaque = YES;
[label addSubview:myImage];
}
//CF類型變量不適用ARC,需要釋放
CFRelease(results);
CFRelease(addressBook);
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“ios如何實現(xiàn)通訊錄聯(lián)系人各屬性獲取”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。