IOS提供了直接获取手机联系人信息的API,我们需要导入该包addressbook.framework;此包并没有包含在corefoundation.framework里;
而我们通常使用的是Foundation库,这辆个库的区别就是CoreFoundation库是一个C框架,但是这两个库里的内容是可以简单的进行转换的,
比如:CFString cstr = (CFString)@"NSStringTest";NSDate *date = (NSDate *)cfdate;CoreFoundation里也有类似retain和release的方法,CFRetain和CFRelease。
首先介绍几个类:
1:ABAddressBookRef:ABAddressBookRef像一个管理员,使用过Sqlite的朋友都知道Sqlite3对象的作用,可以说ABAddressBookRef实例的作用和sqlite3的实例的性质一样,都类似一个管理者,来管理这个通讯录,并给你提供一个访问通讯录的通道,并执行保存操作。初始化一个ABAddressBookRef的实例ABAddressBookCreate( )。
2:ABRecordRef:记录。一个属性的集合。类似通讯录中一个个的人,它包含这个人的所有属性,比如First and Last name,图片信息,电话号码,邮箱等等。初始化一个ABRecordRef的方法就是:ABRecordRef person = ABPersonCreate();
我们需要的信息主要包含在下面这2个类种:
1:ABPerson 联系人信息,包含姓名,电话号码,地址信息,照片等
2:ABGroup 联系人分组信息,包含组名,数量等
最后说明:
1:返回值使用CoreFoundation是一样的:可以认为CFString*=NSString,CFArray*=NSArray,ABMultiValueRef*=Dictionary,CFError*=NSError等等,这些都可以互相转换
2:返回联系人属性分为两种:一种是拥有唯一值的属性比如姓名,生日,性别等一般用CFString返回,一种是多值属性比如电话号码,地址等一般用ABMultiValue*等返回。
3:常使用的方法和宏:
ABRecordRef record=CFArrayGetValueAtIndex(CFArr,0);实际上和NSArr类似;
ABMultiValueRef mulitValue=ABRecordCopyValue(record,kABPersonPhoneProperty);获取电话号码,可能回多个;
像上面kABPersonPhoneProperty这种宏有许多,可以用来取名kABPersonFirstNameProper
常用情景:
ABAddressBookRef address=ABAddressBookCreate();//读取手机通讯录
CFArrayRef allperson =ABAddressBookCopyArrayOfAllPeople(addressBook);//取全部联系人
NSArray * persons=(NSArray*)ABAddressBookCopyPeopleWithName(address,CFStringRef);//取指定姓名的联系人
一:添加联系人
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();//初始化
ABRecordRef newPerson = ABPersonCreate();
CFErrorRef error = NULL;
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, firsrName, &error);//以下几行设置用户基本属性,姓名,公司
ABRecordSetValue(newPerson, kABPersonLastNameProperty, lastName, &error);
ABRecordSetValue(newPerson, kABPersonOrganizationProperty, organization, &error);
ABRecordSetValue(newPerson, kABPersonFirstNamePhoneticProperty, firstNamePhonetic, &error);
ABRecordSetValue(newPerson, kABPersonLastNamePhoneticProperty, lastNamePhonetic, &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);//号码可以是多个,用了多值属性
ABMultiValueAddValueAndLabel(multiPhone, homeFAX, kABPersonPhoneHomeFAXLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, mobile, kABPersonPhoneMobileLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error);
CFRelease(multiPhone);
ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);//邮件可以是多个,用了多值属性
ABMultiValueAddValueAndLabel(multiEmail, email, kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);
CFRelease(multiEmail);
NSData *dataRef = UIImagePNGRepresentation(headImage);//设置头像
ABPersonSetImageData(newPerson, (CFDataRef)dataRef, &error);
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
CFRelease(newPerson);
CFRelease(iPhoneAddressBook);
二:删除联系人
CFErrorRef error = NULL;
ABRecordRef deletedPeople = ABAddressBookGetPersonWithRecordID(addressBook, recordID);
if (deletedPeople) {
ABAddressBookRemoveRecord(addressBook, deletedPeople, &error);
ABAddressBookSave(addressBook, &error);
}
三:获取分组信息 CFArrayRef array = ABAddressBookCopyArrayOfAllGroups(address);
for (id group in (NSArray *)array) {
NSLog(@"group name = %@", ABRecordCopyValue(group, kABGroupNameProperty));
NSLog(@"group id = %d", ABRecordGetRecordID(group));
}
四:删除分组信息 ABRecordRef deletedGroup = ABAddressBookGetGroupWithRecordID(address, RecordID);
ABAddressBookRemoveRecord(address, deletedGroup, nil);
ABAddressBookSave(address, nil);
五:添加分组信息 ABRecordRef newGroup = ABGroupCreate();
ABRecordSetValue(newGroup, kABGroupNameProperty, groupName, nil);
ABAddressBookAddRecord(address, newGroup, nil);
ABAddressBookSave(address, nil);
六:常用基本属性
CFArrayRef allperson =ABAddressBookCopyArrayOfAllPeople(addressBook);
for (id person in (__bridge NSArray *)allperson) {
ABMultiValueRef cfphone = ABRecordCopyValue((__bridge ABRecordRef)person, kABPersonPhoneProperty);
//获取firstname
NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
//获取lastname kABPersonLastNameProperty
//获取middlename kABPersonMiddleNameProperty
//获取前缀 kABPersonPrefixProperty
//获取后缀 kABPersonSuffixProperty
//获取昵称 kABPersonNicknameProperty
//获取firstname拼音音标 kABPersonFirstNamePhoneticProperty
//获取lastname拼音音标 kABPersonLastNamePhoneticProperty
//获取middlename拼音音标 kABPersonMiddleNamePhoneticProperty
//获取organization公司 kABPersonOrganizationProperty
//获取jobtitle工作 kABPersonJobTitleProperty
//获取department部门 kABPersonDepartmentProperty
//获取birthday生日 kABPersonBirthdayProperty
//获取note备忘录 kABPersonNoteProperty
//第一次添加该条记录的时间 kABPersonCreationDateProperty
//最后一次修改該条记录的时间 kABPersonModificationDateProperty
//获取照片
NSData *image = (NSData*)ABPersonCopyImageData(person);
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)];
[myImage setImage:[UIImage imageWithData:image]];
//==================以下是多值获取=========================
//获取email多值
ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);
int emailcount = ABMultiValueGetCount(email);
for (int i = 0; i < emailcount; i++)
{
//获取email类型 Label
NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, i));
//获取email值
NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, i);
}
//获取地址多值
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
int count = ABMultiValueGetCount(address);
for(int j = 0; j < count; j++)
{
//获取地址类型Label
NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);
//获取該label下的地址6属性
NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);
NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];
NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey];
NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey];
NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];
NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];
NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];
}
//获取dates多值
ABMultiValueRef dates = ABRecordCopyValue(person, kABPersonDateProperty);
int datescount = ABMultiValueGetCount(dates);
for (int k = 0; k < datescount; k++)
{
//获取dates Label
NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, k));
//获取dates值
NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, k);
}
//获取kind值
CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);
if (recordType == kABPersonKindOrganization) {
// it's a company
} else {
// 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下的2属性
NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);
NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];
NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];
}
//获取电话多值
ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
for (int m = 0; m<ABMultiValueGetCount(phone); m++)
{
//获取电话Label类型
NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, m));
//获取該Label下的电话值
NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, m);
}
//获取URL多值
ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);
for (int n = 0; n < ABMultiValueGetCount(url); n++)
{
//获取电话Label
NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, n));
//获取該Label下的电话值
NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(url,n);
}
}