AddressBook 代码详解
为了调用系统的通讯录界面与相应功能,需要引入AddressBook.framework
同时,在源文件中需要包含同文件:
- #import
<</span>AddressBook/AddressBook.h> - #import
<</span>AddressBookUI/AddressBookUI.h>
读取手机通讯录
ABAddressBookRef
读取联系人 小明
CFStringRef
NSArray
people就是名字为小明的联系人数组。默认对象是CFArray,取长度方法为:CFArrayGetCount(people)
为了方便强制转换成了NSArray
其中一个小明
- if(people
!= nil && [people count]>0){ ABRecordRef aXiaoming0 = CFArrayGetValueAtIndex(people,0); - }
- //获取小明0的名字
- CFStringRef
cfname = ABRecordCopyValue(aXiaoming0, kABPersonFirstNameProper ty); - //获取小明0的电话信息
- ABMultiValueRef
cfphone = ABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty); - //获取小明0的第0个电话类型:(比如
工作,住宅,iphone,移动电话等) - CFStringRef
leixin = ABMultiValueCopyLabelAtI ndex(cfphone,0); - //获取小明0的第3个电话号码:(使用前先判断长度ABMultiValueGetCount(cfphone)>4)
- CFStringRef
haoma = ABMultiValueCopyValueAtI ndex(cfphone,3); - //添加一个联系人
- CFErrorRef
anError = NULL; - ABRecordRef
aContact = ABPersonCreate();//联系人 - //名字
- NSString*
name = @"小利"; - CFStringRef
cfsname = CFStringCreateWithCStrin g( kCFAllocatorDefault, [name UTF8String], kCFStringEncodingUTF8); - ABRecordSetValue(aContact,
kABPersonFirstNameProper ty, cfsname, &anError);//写入名字进联系人 - //号码
- ABMultiValueRef
phone =ABMultiValueCreateMutabl e(kABMultiStringPropertyTy pe); - ABMultiValueAddValueAndL
abel(phone, @“13800138000”,kABPersonPhoneMobileLabe l, NULL);//添加移动号码0 - ABMultiValueAddValueAndL
abel(phone, @“18688888888”,kABPersonPhoneIPhoneLabe l, NULL);//添加iphone号码1 - //⋯⋯
添加多个号码 - ABRecordSetValue(aContact,
kABPersonPhoneProperty, phone, &anError);//写入全部号码进联系人 - ABAddressBookAddRecord(addressBook,
aContact, &anError);//写入通讯录 - ABAddressBookSave(addressBook,
&error);//保存 - //注意释放各数据
- CFRelease(cfsname);
- CFRelease(phone);
- CFRelease(aContact);
- CFRelease(addressBook);
获取所有联系人
- CFArrayRef
allperson =ABAddressBookCopyArrayOf AllPeople(addressBook); - for
(id person in (NSArray *)allperson) { - }
添加联系人
- //name
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); ABRecordRef newPerson = ABPersonCreate(); CFErrorRef error = NULL; ABRecordSetValue(newPerson, kABPersonFirstNameProper ty, firsrName.text, &error); ABRecordSetValue(newPerson, kABPersonLastNamePropert y, lastName.text, &error); ABRecordSetValue(newPerson, kABPersonOrganizationPro perty, company.text, &error); ABRecordSetValue(newPerson, kABPersonFirstNamePhonet icProperty, firsrNamePY.text, &error); ABRecordSetValue(newPerson, kABPersonLastNamePhoneti cProperty, lastNamePY.text, &error); //phone number ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutabl e(kABMultiStringPropertyTy pe); ABMultiValueAddValueAndL abel(multiPhone, houseNumber.text, kABPersonPhoneHomeFAXLab el, NULL); ABMultiValueAddValueAndL abel(multiPhone, mobileNumber.text, kABPersonPhoneMobileLabe l, NULL); ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error); CFRelease(multiPhone); //email ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutabl e(kABMultiStringPropertyTy pe); ABMultiValueAddValueAndL abel(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);
删除联系人
- CFErrorRef
error = NULL; - ABRecordRef
oldPeople = ABAddressBookGetPersonWi thRecordID(iPhoneAddressBook, recordID); - if
(!oldPeople) { return; - }
- ABAddressBookRef
iPhoneAddressBook = ABAddressBookCreate(); - ABAddressBookRemoveRecor
d(iPhoneAddressBook, oldPeople, &error); - ABAddressBookSave(iPhoneAddressBook,
&error); - CFRelease(iPhoneAddressBook);
- CFRelease(oldPeople);
获取所有组
- CFArrayRef
array = ABAddressBookCopyArrayOf AllGroups(iPhoneAddressBook); - for
(id group in (NSArray *)array) { NSLog(@"group name = %@", ABRecordCopyValue(group, kABGroupNameProperty)); NSLog(@"group id = %d", ABRecordGetRecordID(group)); - }
删除组
- ABAddressBookRef
iPhoneAddressBook = ABAddressBookCreate(); - ABRecordRef
oldGroup = ABAddressBookGetGroupWit hRecordID(iPhoneAddressBook, RecordID); - ABAddressBookRemoveRecor
d(iPhoneAddressBook, oldGroup, nil); - ABAddressBookSave(iPhoneAddressBook,
nil); - CFRelease(iPhoneAddressBook);
- CFRelease(oldGroup);
添加组
- ABAddressBookRef
iPhoneAddressBook = ABAddressBookCreate(); - ABRecordRef
newGroup = ABGroupCreate(); - ABRecordSetValue(newGroup,
kABGroupNameProperty, groupName.text, nil); - ABAddressBookAddRecord(iPhoneAddressBook,
newGroup, nil); - ABAddressBookSave(iPhoneAddressBook,
nil); - CFRelease(newGroup);
- CFRelease(iPhoneAddressBook);
获得通讯录中联系人的所有属性
- ABAddressBookRef
addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOf AllPeople(addressBook); for(int i = 0; i <</span> CFArrayGetCount(results); i++) { ABRecordRef person = CFArrayGetValueAtIndex(results, i); //读取firstname NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProper ty); if(personName != nil) textView.text = [textView.text stringByAppendingFormat:@"n姓名:%@n",personName]; //读取lastname NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePropert y); if(lastname != nil) textView.text = [textView.text stringByAppendingFormat:@"%@n",lastname]; //读取middlename NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePrope rty); 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, kABPersonNicknamePropert y); if(nickname != nil) textView.text = [textView.text stringByAppendingFormat:@"%@n",nickname]; //读取firstname拼音音标 NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhonet icProperty); if(firstnamePhonetic != nil) textView.text = [textView.text stringByAppendingFormat:@"%@n",firstnamePhonetic]; //读取lastname拼音音标 NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneti cProperty); if(lastnamePhonetic != nil) textView.text = [textView.text stringByAppendingFormat:@"%@n",lastnamePhonetic]; //读取middlename拼音音标 NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhone ticProperty); if(middlenamePhonetic != nil) textView.text = [textView.text stringByAppendingFormat:@"%@n",middlenamePhonetic]; //读取organization公司 NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationPro perty); if(organization != nil) textView.text = [textView.text stringByAppendingFormat:@"%@n",organization]; //读取jobtitle工作 NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitlePropert y); if(jobtitle != nil) textView.text = [textView.text stringByAppendingFormat:@"%@n",jobtitle]; //读取department部门 NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentPrope rty); if(department != nil) textView.text = [textView.text stringByAppendingFormat:@"%@n",department]; //读取birthday生日 NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayPropert y); 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, kABPersonCreationDatePro perty); NSLog(@"第一次添加该条记录的时间%@n",firstknow); //最后一次修改該条记录的时间 NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDat eProperty); NSLog(@"最后一次修改該条记录的时间%@n",lastknow); //获取email多值 ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty); int emailcount = ABMultiValueGetCount(email); for (int x = 0; x <</span> emailcount; x++) { //获取email Label NSString* emailLabel = (NSString*)ABAddressBookCopyLocaliz edLabel(ABMultiValueCopyLabelAtI ndex(email, x)); //获取email值 NSString* emailContent = (NSString*)ABMultiValueCopyValueAtI ndex(email, x); textView.text = [textView.text stringByAppendingFormat:@"%@:%@n",emailLabel,emailContent]; } //读取地址多值 ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty ); int count = ABMultiValueGetCount(address); for(int j = 0; j <</span> count; j++) { //获取地址Label NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtI ndex(address, j); textView.text = [textView.text stringByAppendingFormat:@"%@n",addressLabel]; //获取該label下的地址6属性 NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtI ndex(address, j); NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryK ey]; 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 *)kABPersonAddressStreetKe y]; 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 *)kABPersonAddressCountryC odeKey]; 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 <</span> datescount; y++) { //获取dates Label NSString* datesLabel = (NSString*)ABAddressBookCopyLocaliz edLabel(ABMultiValueCopyLabelAtI ndex(dates, y)); //获取dates值 NSString* datesContent = (NSString*)ABMultiValueCopyValueAtI ndex(dates, y); textView.text = [textView.text stringByAppendingFormat:@"%@:%@n",datesLabel,datesContent]; } //获取kind值 CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty); if (recordType == kABPersonKindOrganizatio n) { // it's a company NSLog(@"it's a companyn"); } else { // it's a person, resource, or room NSLog(@"it's a person, resource, or roomn"); } //获取IM多值 ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageP roperty); for (int l = 1; l <</span> ABMultiValueGetCount(instantMessage); l++) { //获取IM Label NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtI ndex(instantMessage, l); textView.text = [textView.text stringByAppendingFormat:@"%@n",instantMessageLabel]; //获取該label下的2属性 NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtI ndex(instantMessage, l); NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageU sernameKey]; if(username != nil) textView.text = [textView.text stringByAppendingFormat:@"username:%@n",username]; NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageS erviceKey]; if(service != nil) textView.text = [textView.text stringByAppendingFormat:@"service:%@n",service]; } //读取电话多值 ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty); for (int k = 0; k<</span>ABMultiValueGetCount(phone); k++) { //获取电话Label NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocaliz edLabel(ABMultiValueCopyLabelAtI ndex(phone, k)); //获取該Label下的电话值 NSString * personPhone = (NSString*)ABMultiValueCopyValueAtI ndex(phone, k); textView.text = [textView.text stringByAppendingFormat:@"%@:%@n",personPhoneLabel,personPhone]; } //获取URL多值 ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty); for (int m = 0; m <</span> ABMultiValueGetCount(url); m++) { //获取电话Label NSString * urlLabel = (NSString*)ABAddressBookCopyLocaliz edLabel(ABMultiValueCopyLabelAtI ndex(url, m)); //获取該Label下的电话值 NSString * urlContent = (NSString*)ABMultiValueCopyValueAtI ndex(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);
AddressBook 代码详解相关推荐
- 【CV】Pytorch一小时入门教程-代码详解
目录 一.关键部分代码分解 1.定义网络 2.损失函数(代价函数) 3.更新权值 二.训练完整的分类器 1.数据处理 2. 训练模型(代码详解) CPU训练 GPU训练 CPU版本与GPU版本代码区别 ...
- html5代码转换为视频,HTML5中的视频代码详解
摘要 腾兴网为您分享:HTML5中的视频代码详解,智学网,云闪付,易推广,小红书等软件知识,以及360win10,流量魔盒,fitbit,上港商城,安卓2.3.7,全民惠,五年级下册英语单词表图片,t ...
- js php base64,JavaScript实现Base64编码与解码的代码详解
本篇文章给大家分享的是jJavaScript实现Base64编码与解码的代码详解,内容挺不错的,希望可以帮助到有需要的朋友 一.加密解密方法使用//1.加密 var str = '124中文内容'; ...
- yii mysql 事务处理_Yii2中事务的使用实例代码详解
前言 一般我们做业务逻辑,都不会仅仅关联一个数据表,所以,会面临事务问题. 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全 ...
- 代码详解|tensorflow实现 聊天AI--PigPig养成记(1)
Chapter1.代码详解 完整代码github链接,Untitled.ipynb文件内. [里面的测试是还没训练完的时候测试的,今晚会更新训练完成后的测试结果] 修复了网上一些代码的bug,解决了由 ...
- vue build text html,Vue中v-text / v-HTML使用实例代码详解_放手_前端开发者
废话少说,代码如下所述: /p> 显示123 /p> 补充:vuejs {{}},v-text 和 v-html的区别 {{message}} let app = new Vue({ el ...
- sift计算描述子代码详解_代码详解——如何计算横向误差?
在路径跟踪控制的论文中,我们常会看到判断精确性的指标,即横向误差和航向误差,那么横向误差和航向误差如何获得? 在前几期代码详解中,参考路径和实际轨迹均由To Workspace模块导出,如图所示: 那 ...
- 委托与事件代码详解与(Object sender,EventArgs e)详解
委托与事件代码详解 using System; using System.Collections.Generic; using System.Text; namespace @Delegate //自 ...
- python怎么画条形图-python绘制条形图方法代码详解
1.首先要绘制一个简单的条形图 import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from ...
- python代码大全表解释-python操作列表的函数使用代码详解
python的列表很重要,学习到后面你会发现使用的地方真的太多了.最近在写一些小项目时经常用到列表,有时其中的方法还会忘哎! 所以为了复习写下了这篇博客,大家也可以来学习一下,应该比较全面和详细了 列 ...
最新文章
- mimo的误码率_揭晓MU-MIMO黑科技!
- Java开发人员最常犯的10个错误,你犯过几个?
- [LOJ#2270][BZOJ4912][SDOI2017]天才黑客
- Java JTable3
- MySQL 十大常用字符串函数
- 群晖 root_群晖洗白简单教程
- 深度学习图像标注工具
- Python刚刚尝试就遇:SyntaxError: invalid syntax
- [算法]直线与圆的交点程序设计
- 专升本高等数学考试知识点汇总(一)
- WIFI共享大师无法开启发射功能
- Adobe Photoshop(Ps)2023软件安装包下载及安装教程(mac+windows多版PS软件) 超级丰富的!
- 区块链公司BitFury与联合国合作开展哈萨克斯坦的森林项目
- 混合硬盘计算机,什么是混合硬盘 什么是hhd硬盘?
- 第五人格服务器正在维护中怎么办,第五人格新联动刚来就出问题,紧急停服维护,这得补偿多少?...
- 金山词霸的字典引擎接口
- wordcloud出错_我在安装wordcloud时出错
- 数字信号处理实践方法 第二版 笔记
- 迅雷7.9.8.4550 Ayu精简绿化版
- HDU4218 IMBA?
热门文章
- 建行网银登录密码被盗,然后遭遇电话诈骗
- 服务器主板型号命令,Linux通过命令查询服务器型号、主板、CPU、内存及硬盘信息...
- java 跳跃表_你真的了解跳跃表吗
- socket长连接的维持
- 支付宝 java 签名_支付宝APP支付(Java后台生成签名具体步骤)
- linux中syscmd用法,M4 宏处理器
- python里的百分号_python里百分号什么意思
- 什么是IDOC,以及IDOC的步骤_小七_新浪博客
- cocos2dx[2.x](9)--编辑框之一CCTextFieldTTF
- cv2批量修改图片大小