ios操作通讯录

为了调用系统的通讯录界面与相应功能,需要引入AddressBook.framework

同时,在源文件中需要包含同文件:

[html] view plaincopy

#import <AddressBook/AddressBook.h>

#import <AddressBookUI/AddressBookUI.h>

读取手机通讯录

ABAddressBookRef addressBook = ABAddressBookCreate();

读取联系人 小明

CFStringRef cfName = CFSTR("小明");

NSArray *people = (NSArray*)ABAddressBookCopyPeopleWithName(myAddressBook, cfName);

people就是名字为小明的联系人数组。默认对象是CFArray,取长度方法为:CFArrayGetCount(people)

为了方便强制转换成了NSArray

其中一个小明

[html] view plaincopy

if(people != nil && [people count]>0){

ABRecordRef aXiaoming0 = CFArrayGetValueAtIndex(people,0);

}

//获取小明0的名字

CFStringRef cfname = ABRecordCopyValue(aXiaoming0, kABPersonFirstNameProperty);

//获取小明0的电话信息

ABMultiValueRef cfphone = ABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty);

//获取小明0的第0个电话类型:(比如 工作,住宅,iphone,移动电话等)

CFStringRef leixin = ABMultiValueCopyLabelAtIndex(cfphone,0);

//获取小明0的第3个电话号码:(使用前先判断长度ABMultiValueGetCount(cfphone)>4)

CFStringRef haoma = ABMultiValueCopyValueAtIndex(cfphone,3);

//添加一个联系人

CFErrorRef anError = NULL;

ABRecordRef aContact = ABPersonCreate();//联系人

//名字

NSString* name = @"小利";

CFStringRef cfsname = CFStringCreateWithCString( kCFAllocatorDefault, [name UTF8String], kCFStringEncodingUTF8);

ABRecordSetValue(aContact, kABPersonFirstNameProperty, cfsname, &anError);//写入名字进联系人

//号码

ABMultiValueRef phone =ABMultiValueCreateMutable(kABMultiStringPropertyType);

ABMultiValueAddValueAndLabel(phone, @“13800138000”,kABPersonPhoneMobileLabel, NULL);//添加移动号码0

ABMultiValueAddValueAndLabel(phone, @“18688888888”,kABPersonPhoneIPhoneLabel, NULL);//添加iphone号码1

//?? 添加多个号码

ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);//写入全部号码进联系人

ABAddressBookAddRecord(addressBook, aContact, &anError);//写入通讯录

ABAddressBookSave(addressBook, &error);//保存

//注意释放各数据

CFRelease(cfsname);

CFRelease(phone);

CFRelease(aContact);

CFRelease(addressBook);

获取所有联系人

[html] view plaincopy

CFArrayRef allperson =ABAddressBookCopyArrayOfAllPeople(addressBook);

for (id person in (NSArray *)allperson) {

}

添加联系人

[html] view plaincopy

//name

ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();

ABRecordRef newPerson = ABPersonCreate();

CFErrorRef error = NULL;

ABRecordSetValue(newPerson, kABPersonFirstNameProperty, firsrName.text, &error);

ABRecordSetValue(newPerson, kABPersonLastNameProperty, lastName.text, &error);

ABRecordSetValue(newPerson, kABPersonOrganizationProperty, company.text, &error);

ABRecordSetValue(newPerson, kABPersonFirstNamePhoneticProperty, firsrNamePY.text, &error);

ABRecordSetValue(newPerson, kABPersonLastNamePhoneticProperty, lastNamePY.text, &error);

//phone number

ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);

ABMultiValueAddValueAndLabel(multiPhone, houseNumber.text, kABPersonPhoneHomeFAXLabel, NULL);

ABMultiValueAddValueAndLabel(multiPhone, mobileNumber.text, kABPersonPhoneMobileLabel, NULL);

ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error);

CFRelease(multiPhone);

//email

ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);

ABMultiValueAddValueAndLabel(multiEmail, email.text, kABWorkLabel, NULL);

ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);

CFRelease(multiEmail);

//picture

NSData *dataRef = UIImagePNGRepresentation(head.image);

ABPersonSetImageData(newPerson, (CFDataRef)dataRef, &error);

ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);

ABAddressBookSave(iPhoneAddressBook, &error);

CFRelease(newPerson);

CFRelease(iPhoneAddressBook);

删除联系人

[html] view plaincopy

CFErrorRef error = NULL;

ABRecordRef oldPeople = ABAddressBookGetPersonWithRecordID(iPhoneAddressBook, recordID);

if (!oldPeople) {

return;

}

ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();

ABAddressBookRemoveRecord(iPhoneAddressBook, oldPeople, &error);

ABAddressBookSave(iPhoneAddressBook, &error);

CFRelease(iPhoneAddressBook);

CFRelease(oldPeople);

获取所有组

[html] view plaincopy

CFArrayRef array = ABAddressBookCopyArrayOfAllGroups(iPhoneAddressBook);

for (id group in (NSArray *)array) {

NSLog(@"group name = %@", ABRecordCopyValue(group, kABGroupNameProperty));

NSLog(@"group id = %d", ABRecordGetRecordID(group));

}

删除组

[html] view plaincopy

ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();

ABRecordRef oldGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook, RecordID);

ABAddressBookRemoveRecord(iPhoneAddressBook, oldGroup, nil);

ABAddressBookSave(iPhoneAddressBook, nil);

CFRelease(iPhoneAddressBook);

CFRelease(oldGroup);

添加组

[html] view plaincopy

ABAddressBookRef  iPhoneAddressBook = ABAddressBookCreate();

ABRecordRef  newGroup = ABGroupCreate();

ABRecordSetValue(newGroup, kABGroupNameProperty, groupName.text, nil);

ABAddressBookAddRecord(iPhoneAddressBook, newGroup, nil);

ABAddressBookSave(iPhoneAddressBook, nil);

CFRelease(newGroup);

CFRelease(iPhoneAddressBook);

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

[html] view plaincopy

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)

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

//读取lastname

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

if(lastname != nil)

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

//读取middlename

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

if(middlename != nil)

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

//读取prefix前缀

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

if(prefix != nil)

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

//读取suffix后缀

NSString *suffix = (NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);

if(suffix != nil)

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

//读取nickname呢称

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

if(nickname != nil)

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

//读取firstname拼音音标

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

if(firstnamePhonetic != nil)

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

//读取lastname拼音音标

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

if(lastnamePhonetic != nil)

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

//读取middlename拼音音标

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

if(middlenamePhonetic != nil)

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

//读取organization公司

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

if(organization != nil)

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

//读取jobtitle工作

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

if(jobtitle != nil)

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

//读取department部门

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

if(department != nil)

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

//读取birthday生日

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

if(birthday != nil)

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

//读取note备忘录

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

if(note != nil)

textView.text = [textView.text stringByAppendingFormat:@"%@\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);

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

textView.text = [textView.text stringByAppendingFormat:@"%@:%@\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.text stringByAppendingFormat:@"%@\n",addressLabel];

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

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

NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];

if(country != nil)

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

NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];

if(city != nil)

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

NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];

if(state != nil)

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

NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];

if(street != nil)

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

NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];

if(zip != nil)

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

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

if(coutntrycode != nil)

textView.text = [textView.text stringByAppendingFormat:@"国家编号:%@\n",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);

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

}

//获取kind值

CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);

if (recordType == kABPersonKindOrganization) {

// it‘s a company

NSLog(@"it‘s a company\n");

} else {

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

NSLog(@"it‘s a person, resource, or room\n");

}

//获取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.text stringByAppendingFormat:@"%@\n",instantMessageLabel];

//获取該label下的2属性

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

NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];

if(username != nil)

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

NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];

if(service != nil)

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

textView.text = [textView.text stringByAppendingFormat:@"%@:%@\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.text stringByAppendingFormat:@"%@:%@\n",urlLabel,urlContent];

}

//读取照片

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

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

[myImage setImage:[UIImage imageWithData:image]];

myImage.opaque = YES;

[textView addSubview:myImage];

}

CFRelease(results);

CFRelease(addressBook);

时间: 2024-11-16 21:57:46

ios操作通讯录的相关文章

iOS 修改通讯录联系人地址(address)崩溃原因分析

目前项目中需要对iOS系统通讯录进行读取,修改操作.在进行对地址修改的时候,出现了一个奇怪现象: ● 如果contact没有address字段(或者一个全新的contact),对它的address进行修改是可以成功的, ● 如果这个人有过address字段,此时对它就行修改就崩溃.控制台打出: *** -[CFString release]: message sent to deallocated instance 0x81d26f0 这应该是一个僵尸对象,重复释放某一个对象.首先我对修改通讯录

iOS.访问通讯录.01.读取联系人信息

1.相关函数介绍 1.创建通讯录对象函数 ABAddressBookRef ABAddressBookCreateWithOptions( CFDictionaryRef options, CFErrorRef *error ); 例子: CFErrorRef error = NULL; ABAdressBookRef addressBook = ABAdressBookCreateWithOptions(NULL,&error); ABAddressBookRequestAccessWithC

iOS.访问通讯录.00.概述

1.移动设备上都有一个很重要的内置数据库 -- 通讯录,苹果把它扩展到了iCloud上,使苹果设备间可以共享通讯录信息. 2.在iOS上,通讯录放在SQLite3数据库中,但是应用之间不能直接访问,也就是说我们自己编写的应用不能采用数据持久化技术直接访问通讯录数据库.为了实现通讯录数据库的访问,苹果开放了一些专门的API. 3.处于安全考虑,iOS6之后的应用访问通讯录时,需要获得用户的授权,与其他应用(如定位服务授权)不同的是,通讯录对一个应用只授权一次,即便是这个应用删除后重新安装,也不必再

iOS中通讯录电话号码空格问题

今天在读取通讯录的时候,读取到的手机号码格式为* (***) ***-****的,乍看下,数字中间有空格."-".(.)的非数字字符. 然后我就打算替换这些非数字字符,结果替换完,发现空格还是在,单步调试下,替换也运行了,打印了下号码,发现其实那不是空格,是"·",这可能算是iOS的小坑吧. iOS中通讯录电话号码空格问题

iOS.访问通讯录.02.写入联系人

一.创建联系人 1.创建联系人记录 ABRecord person = ABPersonCreate(); 2.保存或修改单值属性 bool ABRecordSetValue( ABRecordRef record, ABPropertyID property, CFTypeRef value, CFErrorRef *error ); 例子: CFError error = NULL; // 保存姓名 ABRecordSetValue(person,kABPersonFirstNameProp

ios 获取通讯录的所有信息

iOS获取通讯录全部信息 ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook); for(int i = 0; i < CFArrayGetCount(results); i++) { ABRecordRef person = CFArrayGetValueAtIndex(results, i); //读取f

iOS开发------操作通讯录(AddressBook篇)&amp;通讯录UI(AddressBookUI篇)

上篇博文简要的介绍了如何使用AddessBook.framework来获取系统通讯录,但有时候又想对其做修改怎么办,那么这篇博文就总结一下如何修改系统的通讯录吧. 代码GitHub:https://github.com/YRunIntoLove/YAddressBookUIDemo 修改系统通讯录的方法 两种方法 通过AddressBook.framework的各种函数来完成对AddressBook的操作. 通过AddressBookUI.framework中提供的系统UIViewControl

ios 获得通讯录中联系人的所有属性

//获取通讯录中的所有属性,并存储在 textView 中,已检验,切实可行.兼容io6 和 ios 7 ,而且ios7还没有权限确认提示. -(void)getAddressBook { ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook); for(int i = 0; i < CFArrayGetCou

iOS 访问通讯录

1.构建UI 2.向用户申请通讯录的授权 1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 5 // 请求访问通讯录的权限 6 [self requestAccessAddressBook]; 7 } 8 9 // 请求访问通讯录的权限 10 - (void)requestAccessAddressBook 11 { 12 // 创建通讯录实例对象 13 ABAddressBookRef addressbook = ABAddressBookC