使用AddressBook添加联系人
添加联系人的步骤如下: |
|
1 |
创建ABAddressBookRef,这就得到了对地址簿的引用。 |
2 |
调用ABPersonCreate()函数创建一个空的ABRecordRef,得到一条空的联系人记录 |
3 |
根据需要为ABRecordRef设置属性值。 只设置姓氏、名字等简单信息,则直接调用ABRecordSetValue()函数为ABRecordRef的指定属性设置属性值即可. 设置电话号码、电子邮件等可以指定label的属性,需要先创建一个ABMutableMultiValueRef, 然后调用ABMultiValueAddValueAndLabel()函数向ABMutableMultiValueRef中添加多个值,最后才能调用ABRecordSetValue()函数为ABRecordRef的指定属性设置属性值. |
4 |
调用ABAddressBookAddRecord()函数将指定ABRecordRef记录添加到地址簿中. |
5 |
调用ABAddressBookSave()函数将删除操作保存到底层地址簿中. |
代码片段 |
// 特此说明用的main. storyboard@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad];}- (IBAction)add:(id)sender{// 名 NSString *firstName = self.firstnameField.text; // 姓 NSString *lastName = self.lastnameField.text; NSString *homePhone = self.homePhoneField.text;// 座机 NSString* mobilePhone = self.mobilePhoneField.text :// 手机NSString* workMail = self.workMailField.text ;NSString* privateMail = self. privateMailField.text ;NSString* country = self.countryField.text ;NSString* state = self. stateField.text ;CFErrorRef error = nil;// 创建ABAddressBook,该函数的第1个参数暂时并未使用,直接传入NULL即可.ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, &error);if(!error){ // 请求访问用户地址簿ABAddressBookAccessWithCompletion(ab, ^(bool granted, CFErrorRef error){ // 如果用户允许访问地址簿if(granted){// 创建一条新的记录ABRecordRef rec = ABPersonCreate();// 为rec的kABPersonFirstNameProperty(名字)属性设置值ABRecordSetValue(rec, kABPersonFirstNameProperty, (__bridge CFStringRef)firstName, NULL);// 为rec的kABPresonLastNameProperty(姓氏)属性设置值ABRecordSetValue(rec, kABPersonLastNameProperty, (__bridge CFStringRef)lastName, NULL);// 创建ABMutableMultiValueRef来管理多个电话号码ABMutableMultiValueRef phoneValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);// 为家庭的电话号码添加labelABMultiValueAddValueAndLabel(phoneValue , (__bridge CFTypeRef)homePhone), kABHomeLabel, NULL);// 为移动的电话号码添加labelABMultiValueAddValueAndLabel(phoneValue , (__bridge CFTypeRef)mobilePhone), kABPersonPhoneMobileLabel, NULL); // 为rec的kABPersonPhoneProperty(电话)属性设置值ABRecordSetValue(rec, kABPersonPhoneProperty, phoneValue, NULL);// 创建ABMutableMultiValueRef来管理多个电子邮件ABMutableMultiValueRef mailValue = ABMultiValueCreateMutable(kABPersonEmailProperty);// 为工作的电子邮件添加labelABMultiValueAddValueAndLabel(mailValue , (__bridge CFStringRef)workMail, (__bridge CFStringRef)@”工作”, NULL);// 为私人的电子邮件添加labelABMultiValueAddValueAndLabel(mailValue , (__bridge CFStringRef)privateMail, (__bridge CFStringRef)@”私人”, NULL);// 为rec的kABPersonEmailProperty(电子邮件)属性设置值ABRecordSetValue(rec, kABPersonMailProperty, mailValue, NULL);// 创建ABMutableMultiValueRef来管理多个地址ABMutableMultiValueRef addrValue = ABMultiValueCreateMutable(kABPersonAddressProperty);// 创建一条新的ABRecordRef记录,这条记录不包含任何属性NSDictionary *addrDict = [NSDictionary dictionaryWithObjectsAndKeys:country, kABPersonAddressCountryKey, state, kABPersonAddressStateKey, nil];// 为住址的地址添加labelABMultiValueAddValueAndLabel(addrValue, (__bridge CFTypeRef)addrDict, (__bridge CFStringRef)@”地址”, NULL);// 为rec的kABPersonAddressProperty(地址)属性设置值ABRecordSetValue(rec, kABPersonAddressProperty, addrValue, NULL);BOOL result = ABAddressBookAddRecord(ab, rec, NULL); if(result) { // 将程序所做的修改保存到地址簿中,如果保存成功 if(ABAddressBookSave(ab, NULL)) { [self showAlert:@”成功添加新的联系人”]; } else { [self showAlert:@”添加失败”]; } } } })}- (IBAction)finishEdit:(id)sender{ [sender resignFirstResponder];}- (void)showAlert:(NSString *)msg{ // 使用UIAlertView显示msg信息 [ [ [UIAlertView alloc] initWithTitle:@” 提 示 ” message:msg delegate:nil cancelButtonTitle:@”确定” otherButtonTitles:nil] show ];}@end// 注释:因为iOS系统要求地址必须是{国家:国家值, 省份:省份值, 城市:城市值}这种NSDictionary对象,所以创建了一个NSDictionary对象作为地址值 |
效果图 |