您的位置:首页 > 移动开发 > IOS开发

IOS学习之获取iphone通讯录

2014-03-14 14:26 411 查看
iPhone中联系人的信息是存放在系统数据库中的。数据库中的每一笔记录都是一个ABRecordRef实例。

  通讯录主要存放两种记录:

  1、联系人的信息:是ABPerson类型。主要包括联系人的姓名,电话号码,地址信息。

  2、分组信息:是ABGroup类型。用于将联系人分到不同的组中。它只有一个属性,表示分组名称。

  在viewDidLoad中添加下面代码,获取所有联系人和分组信息:

  ABAddressBookRef addressBook =ABAddressBookCreate();

  CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

  CFArrayRef allGroups =ABAddressBookCopyArrayOfAllGroups(addressBook);

  for (id person in (NSArray *) allPeople)

  [self logContact:person];

  for (id group in (NSArray *) allGroups)

  [self logGroup:group];

  CFRelease(allGroups);

  CFRelease(allPeople);

  CFRelease(addressBook);

  使用ABAddressBookCreate创建一个AddressBook实例。ABAddressBookCopyArrayOfAllPeople和ABAddressBookCopyArrayOfAllGroups查询出所有的联系人和分组信息。通过循环,使用logPerson和logGroup两个方法将信息输出到控制台上。

  logPerson方法:

  CFStringRef name = ABRecordCopyCompositeName(person);ABRecordIDrecId = ABRecordGetRecordID(person);NSLog(@"Person Name: %@RecordID:%d",name, recId);

  logGroup方法:

  CFStringRef name =ABRecordCopyValue(group,kABGroupNameProperty);ABRecordID recId =ABRecordGetRecordID(group);NSLog(@"Group Name: %@RecordID:%d",name, recId);

获取通讯录中联系人所有属性

导入库:AddressBook.framework AddressBookUI.framework

#import<AddressBookUI/AddressBookUI.h>

#import<AddressBook/AddressBook.h>

ABAddressBookRef addressBook =ABAddressBookCreate();

CFArrayRef results =ABAddressBookCopyArrayOfAllPeople(addressBook);

for(int i = 0; i< CFArrayGetCount(results); i++)

{

ABRecordRefperson = CFArrayGetValueAtIndex(results, i);

//读取firstname

NSString*personName =(NSString*)ABRecordCopyValue(person,kABPersonFirstNameProperty);

if(personName!= nil)

textView.text =[textView.textstringByAppendingFormat:@"n姓名:%@n",personName];

//读取lastname

NSString*lastname =(NSString*)ABRecordCopyValue(person,kABPersonLastNameProperty);

if(lastname!= nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",lastname];

//读取middlename

NSString*middlename =(NSString*)ABRecordCopyValue(person,kABPersonMiddleNameProperty);

if(middlename!= nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",middlename];

//读取prefix前缀

NSString*prefix =(NSString*)ABRecordCopyValue(person,kABPersonPrefixProperty);

if(prefix !=nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",prefix];

//读取suffix后缀

NSString*suffix =(NSString*)ABRecordCopyValue(person,kABPersonSuffixProperty);//kABPers*****uffixProperty

if(suffix !=nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",suffix];

//读取nickname呢称

NSString*nickname =(NSString*)ABRecordCopyValue(person,kABPersonNicknameProperty);

if(nickname!= nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",nickname];

//读取firstname拼音音标

NSString*firstnamePhonetic =(NSString*)ABRecordCopyValue(person,kABPersonFirstNamePhoneticProperty);

if(firstnamePhonetic != nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",firstnamePhonetic];

//读取lastname拼音音标

NSString*lastnamePhonetic =(NSString*)ABRecordCopyValue(person,kABPersonLastNamePhoneticProperty);

if(lastnamePhonetic != nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",lastnamePhonetic];

//读取middlename拼音音标

NSString*middlenamePhonetic =(NSString*)ABRecordCopyValue(person,kABPersonMiddleNamePhoneticProperty);

if(middlenamePhonetic != nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",middlenamePhonetic];

//读取organization公司

NSString*organization =(NSString*)ABRecordCopyValue(person,kABPersonOrganizationProperty);

if(organization != nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",organization];

//读取jobtitle工作

NSString*jobtitle =(NSString*)ABRecordCopyValue(person,kABPersonJobTitleProperty);

if(jobtitle!= nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",jobtitle];

//读取department部门

NSString*department =(NSString*)ABRecordCopyValue(person,kABPersonDepartmentProperty);

if(department!= nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",department];

//读取birthday生日

NSDate*birthday =(NSDate*)ABRecordCopyValue(person,kABPersonBirthdayProperty);

if(birthday!= nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",birthday];

//读取note备忘录

NSString*note =(NSString*)ABRecordCopyValue(person,kABPersonNoteProperty);

if(note !=nil)

textView.text =[textView.textstringByAppendingFormat:@"%@n",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);

intemailcount = 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);

textView.text =[textView.textstringByAppendingFormat:@"%@:%@n",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);

textView.text =[textView.textstringByAppendingFormat:@"%@n",addressLabel];

//获取該label下的地址6属性

NSDictionary* personaddress=(NSDictionary*)ABMultiValueCopyValueAtIndex(address, j);

NSString* country = [personaddressvalueForKey:(NSString*)kABPersonAddressCountryKey];

if(country != nil)

textView.text =[textView.textstringByAppendingFormat:@"国家:%@n",country];

NSString* city = [personaddressvalueForKey:(NSString*)kABPersonAddressCityKey];

if(city != nil)

textView.text =[textView.textstringByAppendingFormat:@"城市:%@n",city];

NSString* state = [personaddressvalueForKey:(NSString*)kABPersonAddressStateKey];

if(state != nil)

textView.text =[textView.textstringByAppendingFormat:@"省:%@n",state];

NSString* street = [personaddressvalueForKey:(NSString*)kABPersonAddressStreetKey];

if(street != nil)

textView.text =[textView.textstringByAppendingFormat:@"街道:%@n",street];

NSString* zip = [personaddressvalueForKey:(NSString*)kABPersonAddressZIPKey];

if(zip != nil)

textView.text =[textView.textstringByAppendingFormat:@"邮编:%@n",zip];

NSString* coutntrycode =[personaddress
valueForKey:(NSString*)kABPersonAddressCountryCodeKey];

if(coutntrycode !=nil)

textView.text =[textView.textstringByAppendingFormat:@"国家编号:%@n",coutntrycode];

}

//获取dates多值

ABMultiValueRef dates =ABRecordCopyValue(person,kABPersonDateProperty);

intdatescount = 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);

textView.text =[textView.textstringByAppendingFormat:@"%@:%@n",datesLabel,datesContent];

}

//获取kind值

CFNumberRefrecordType =ABRecordCopyValue(person,kABPersonKindProperty);

if (recordType ==kABPersonKindOrganization) {

// it's a company

NSLog(@"it's acompanyn");

} else {

// it's a person, resource, or room

NSLog(@"it's aperson, resource, or roomn");

}

//获取IM多值

ABMultiValueRef instantMessage =ABRecordCopyValue(person,kABPersonInstantMessageProperty);

for(int l = 1; l <ABMultiValueGetCount(instantMessage); l++)

{

//获取IM Label

NSString* instantMessageLabel =(NSString*)ABMultiValueCopyLabelAtIndex(instantMessage,l);

textView.text =[textView.textstringByAppendingFormat:@"%@n",instantMessageLabel];

//获取該label下的2属性

NSDictionary* instantMessageContent=(NSDictionary*)ABMultiValueCopyValueAtIndex(instantMessage, l);

NSString* username =[instantMessageContentvalueForKey:(NSString*)kABPersonInstantMessageUsernameKey];

if(username !=nil)

textView.text =[textView.textstringByAppendingFormat:@"username:%@n",username];

NSString* service =[instantMessageContentvalueForKey:(NSString*)kABPersonInstantMessageServiceKey];

if(service != nil)

textView.text =[textView.textstringByAppendingFormat:@"service:%@n",service];

}

//读取电话多值

ABMultiValueRef phone =ABRecordCopyValue(person,kABPersonPhoneProperty);

for(int k = 0;k<<spanclass="s5">ABMultiValueGetCount(phone); k++)

{

//获取电话Label

NSString *personPhoneLabel =(NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone,k));

//获取該Label下的电话值

NSString * personPhone =(NSString*)ABMultiValueCopyValueAtIndex(phone,k);

textView.text =[textView.textstringByAppendingFormat:@"%@:%@n",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);

textView.text =[textView.textstringByAppendingFormat:@"%@:%@n",urlLabel,urlContent];

}

//读取照片

NSData *image=(NSData*)ABPersonCopyImageData(person);

UIImageView*myImage = [[UIImageView
alloc]initWithFrame:CGRectMake(200,0,
50, 50)];

[myImagesetImage:[UIImageimageWithData:image]];

myImage.opaque = YES;

[textViewaddSubview:myImage];

}

CFRelease(results);

CFRelease(addressBook);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: