首页 技术 正文
技术 2022年11月22日
0 收藏 817 点赞 4,358 浏览 3379 个字

使用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对象作为地址值

效果图

iOS—–使用AddressBook添加联系人

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,906
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,739
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,492
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,130
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,293