原文地址:AddressBook 代码详解作者:花花猪的代码

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

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

[html] view plaincopy

  1. #import <</span>AddressBook/AddressBook.h> 
  2. #import <</span>AddressBookUI/AddressBookUI.h> 

读取手机通讯录
ABAddressBookRefaddressBook =ABAddressBookCreate();
读取联系人 小明
CFStringRef cfName = CFSTR("小明");
NSArray*people = (NSArray*)ABAddressBookCopyPeopleWithName(myAddressBook, cfName);
people就是名字为小明的联系人数组。默认对象是CFArray,取长度方法为:CFArrayGetCountpeople
为了方便强制转换成了NSArray

其中一个小明

[html] view plaincopy

  1. if(people != nil && [people count]>0){  
  2.        ABRecordRef aXiaoming0CFArrayGetValueAtIndex(people,0);  
  3.  
  4.  
  5. //获取小明0的名字  
  6. CFStringRef cfnameABRecordCopyValue(aXiaoming0, kABPersonFirstNameProperty);  
  7.  
  8. //获取小明0的电话信息  
  9. ABMultiValueRef cfphoneABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty);  
  10.  
  11. //获取小明0的第0个电话类型:(比如 工作,住宅,iphone,移动电话等)  
  12. CFStringRef leixinABMultiValueCopyLabelAtIndex(cfphone,0);  
  13.  
  14. //获取小明0的第3个电话号码:(使用前先判断长度ABMultiValueGetCount(cfphone)>4)  
  15. CFStringRef haomaABMultiValueCopyValueAtIndex(cfphone,3);  
  16.  
  17. //添加一个联系人  
  18.  
  19. CFErrorRef anErrorNULL 
  20. ABRecordRef aContactABPersonCreate();//联系人  
  21.  
  22. //名字  
  23. NSString* name@"小利";  
  24. CFStringRef cfsnameCFStringCreateWithCStringkCFAllocatorDefault, [name UTF8String], kCFStringEncodingUTF8);  
  25. ABRecordSetValue(aContact, kABPersonFirstNameProperty, cfsname, &anError);//写入名字进联系人  
  26.  
  27. //号码  
  28. ABMultiValueRef phone=ABMultiValueCreateMutable(kABMultiStringPropertyType);  
  29. ABMultiValueAddValueAndLabel(phone, @“13800138000”,kABPersonPhoneMobileLabel, NULL);//添加移动号码0  
  30. ABMultiValueAddValueAndLabel(phone, @“18688888888”,kABPersonPhoneIPhoneLabel, NULL);//添加iphone号码1  
  31. //⋯⋯ 添加多个号码  
  32.  
  33. ABRecordSetValue(aContact, kABPersonPhoneProperty, phone, &anError);//写入全部号码进联系人  
  34.  
  35. ABAddressBookAddRecord(addressBook, aContact, &anError);//写入通讯录  
  36. ABAddressBookSave(addressBook, &error);//保存  
  37. //注意释放各数据  
  38. CFRelease(cfsname);  
  39. CFRelease(phone);  
  40. CFRelease(aContact);  
  41. CFRelease(addressBook);  


获取所有联系人


[html] view plaincopy

  1. CFArrayRef allperson=ABAddressBookCopyArrayOfAllPeople(addressBook);  
  2. for (id person in (NSArray *)allperson)  
  3.  

添加联系人

[html] view plaincopy

  1. //name  
  2. ABAddressBookRef iPhoneAddressBookABAddressBookCreate();  
  3. ABRecordRef newPersonABPersonCreate();  
  4. CFErrorRef errorNULL 
  5. ABRecordSetValue(newPerson, kABPersonFirstNameProperty, firsrName.text, &error);  
  6. ABRecordSetValue(newPerson, kABPersonLastNameProperty, lastName.text, &error);  
  7. ABRecordSetValue(newPerson, kABPersonOrganizationProperty, company.text, &error);  
  8. ABRecordSetValue(newPerson, kABPersonFirstNamePhoneticProperty, firsrNamePY.text, &error);  
  9. ABRecordSetValue(newPerson, kABPersonLastNamePhoneticProperty, lastNamePY.text, &error);  
  10. //phone number  
  11. ABMutableMultiValueRef multiPhoneABMultiValueCreateMutable(kABMultiStringPropertyType);  
  12. ABMultiValueAddValueAndLabel(multiPhone, houseNumber.text, kABPersonPhoneHomeFAXLabel, NULL);  
  13. ABMultiValueAddValueAndLabel(multiPhone, mobileNumber.text, kABPersonPhoneMobileLabel, NULL);  
  14. ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error);  
  15. CFRelease(multiPhone);  
  16. //email  
  17. ABMutableMultiValueRef multiEmailABMultiValueCreateMutable(kABMultiStringPropertyType);  
  18. ABMultiValueAddValueAndLabel(multiEmail, email.text, kABWorkLabel, NULL);  
  19. ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);  
  20. CFRelease(multiEmail);  
  21. //picture  
  22. NSData *dataRefUIImagePNGRepresentation(head.image);  
  23. ABPersonSetImageData(newPerson, (CFDataRef)dataRef, &error);  
  24. ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);  
  25. ABAddressBookSave(iPhoneAddressBook, &error);  
  26. CFRelease(newPerson);  
  27. CFRelease(iPhoneAddressBook);  


删除联系人

[html] view plaincopy

  1. CFErrorRef errorNULL 
  2. ABRecordRef oldPeopleABAddressBookGetPersonWithRecordID(iPhoneAddressBook, recordID);  
  3. if (!oldPeople)  
  4.    return;  
  5.  
  6. ABAddressBookRef iPhoneAddressBookABAddressBookCreate();  
  7. ABAddressBookRemoveRecord(iPhoneAddressBook, oldPeople, &error);  
  8. ABAddressBookSave(iPhoneAddressBook, &error);  
  9. CFRelease(iPhoneAddressBook);  
  10. CFRelease(oldPeople);  


获取所有组

[html] view plaincopy

  1. CFArrayRef arrayABAddressBookCopyArrayOfAllGroups(iPhoneAddressBook);  
  2. for (id group in (NSArray *)array)  
  3.    NSLog(@"group name%@", ABRecordCopyValue(group, kABGroupNameProperty));  
  4.    NSLog(@"group id%d", ABRecordGetRecordID(group));  
  5.  

删除组

[html] view plaincopy

  1. ABAddressBookRef iPhoneAddressBookABAddressBookCreate();  
  2. ABRecordRef oldGroupABAddressBookGetGroupWithRecordID(iPhoneAddressBook, RecordID);  
  3. ABAddressBookRemoveRecord(iPhoneAddressBook, oldGroup, nil);  
  4. ABAddressBookSave(iPhoneAddressBook, nil);  
  5. CFRelease(iPhoneAddressBook);  
  6. CFRelease(oldGroup);  




添加组

[html] view plaincopy

  1. ABAddressBookRef  iPhoneAddressBookABAddressBookCreate();  
  2. ABRecordRef  newGroupABGroupCreate();  
  3. ABRecordSetValue(newGroup, kABGroupNameProperty, groupName.text, nil);  
  4. ABAddressBookAddRecord(iPhoneAddressBook, newGroup, nil);  
  5. ABAddressBookSave(iPhoneAddressBook, nil);  
  6. CFRelease(newGroup);  
  7. CFRelease(iPhoneAddressBook);  


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

[html] view plaincopy

  1. ABAddressBookRef addressBookABAddressBookCreate();  
  2. CFArrayRef resultsABAddressBookCopyArrayOfAllPeople(addressBook);  
  3. for(int i0<</span>CFArrayGetCount(results); i++)  
  4.  
  5.     ABRecordRef personCFArrayGetValueAtIndex(results, i);  
  6.     //读取firstname  
  7.     NSString *personName(NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);  
  8.     if(personName != nil)  
  9.         textView.text[textView.text stringByAppendingFormat:@"n姓名:%@n",personName];  
  10.     //读取lastname  
  11.     NSString *lastname(NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);  
  12.     if(lastname != nil)  
  13.         textView.text[textView.text stringByAppendingFormat:@"%@n",lastname];  
  14.     //读取middlename  
  15.     NSString *middlename(NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);  
  16.     if(middlename != nil)  
  17.         textView.text[textView.text stringByAppendingFormat:@"%@n",middlename];  
  18.     //读取prefix前缀  
  19.     NSString *prefix(NSString*)ABRecordCopyValue(person, kABPersonPrefixProperty);  
  20.     if(prefix != nil)  
  21.         textView.text[textView.text stringByAppendingFormat:@"%@n",prefix];  
  22.     //读取suffix后缀  
  23.     NSString *suffix(NSString*)ABRecordCopyValue(person, kABPersonSuffixProperty);  
  24.     if(suffix != nil)  
  25.         textView.text[textView.text stringByAppendingFormat:@"%@n",suffix];  
  26.     //读取nickname呢称  
  27.     NSString *nickname(NSString*)ABRecordCopyValue(person, kABPersonNicknameProperty);  
  28.     if(nickname != nil)  
  29.         textView.text[textView.text stringByAppendingFormat:@"%@n",nickname];  
  30.     //读取firstname拼音音标  
  31.     NSString *firstnamePhonetic(NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);  
  32.     if(firstnamePhonetic != nil)  
  33.         textView.text[textView.text stringByAppendingFormat:@"%@n",firstnamePhonetic];  
  34.     //读取lastname拼音音标  
  35.     NSString *lastnamePhonetic(NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);  
  36.     if(lastnamePhonetic != nil)  
  37.         textView.text[textView.text stringByAppendingFormat:@"%@n",lastnamePhonetic];  
  38.     //读取middlename拼音音标  
  39.     NSString *middlenamePhonetic(NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);  
  40.     if(middlenamePhonetic != nil)  
  41.         textView.text[textView.text stringByAppendingFormat:@"%@n",middlenamePhonetic];  
  42.     //读取organization公司  
  43.     NSString *organization(NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);  
  44.     if(organization != nil)  
  45.         textView.text[textView.text stringByAppendingFormat:@"%@n",organization];  
  46.     //读取jobtitle工作  
  47.     NSString *jobtitle(NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);  
  48.     if(jobtitle != nil)  
  49.         textView.text[textView.text stringByAppendingFormat:@"%@n",jobtitle];  
  50.     //读取department部门  
  51.     NSString *department(NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);  
  52.     if(department != nil)  
  53.         textView.text[textView.text stringByAppendingFormat:@"%@n",department];  
  54.     //读取birthday生日  
  55.     NSDate *birthday(NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);  
  56.     if(birthday != nil)  
  57.         textView.text[textView.text stringByAppendingFormat:@"%@n",birthday];  
  58.     //读取note备忘录  
  59.     NSString *note(NSString*)ABRecordCopyValue(person, kABPersonNoteProperty);  
  60.     if(note != nil)  
  61.         textView.text[textView.text stringByAppendingFormat:@"%@n",note];  
  62.     //第一次添加该条记录的时间  
  63.     NSString *firstknow(NSString*)ABRecordCopyValue(person, kABPersonCreationDateProperty);  
  64.     NSLog(@"第一次添加该条记录的时间%@n",firstknow);  
  65.     //最后一次修改該条记录的时间  
  66.     NSString *lastknow(NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);  
  67.     NSLog(@"最后一次修改該条记录的时间%@n",lastknow);  
  68.       
  69.     //获取email多值  
  70.     ABMultiValueRef emailABRecordCopyValue(person, kABPersonEmailProperty);  
  71.     int emailcountABMultiValueGetCount(email);  
  72.     for (int x0<</span>emailcountx++)  
  73.      
  74.         //获取email Label  
  75.         NSString* emailLabel(NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));  
  76.         //获取email值  
  77.         NSString* emailContent(NSString*)ABMultiValueCopyValueAtIndex(email, x);  
  78.         textView.text[textView.text stringByAppendingFormat:@"%@:%@n",emailLabel,emailContent];  
  79.      
  80.     //读取地址多值  
  81.     ABMultiValueRef addressABRecordCopyValue(person, kABPersonAddressProperty);  
  82.     int countABMultiValueGetCount(address);  
  83.       
  84.     for(int j0<</span>countj++)  
  85.      
  86.         //获取地址Label  
  87.         NSString* addressLabel(NSString*)ABMultiValueCopyLabelAtIndex(address, j);  
  88.         textView.text[textView.text stringByAppendingFormat:@"%@n",addressLabel];  
  89.         //获取該label下的地址6属性  
  90.         NSDictionary* personaddress=(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);  
  91.         NSString* country[personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];  
  92.         if(country != nil)  
  93.             textView.text[textView.text stringByAppendingFormat:@"国家:%@n",country];  
  94.         NSString* city[personaddress valueForKey:(NSString *)kABPersonAddressCityKey];  
  95.         if(city != nil)  
  96.             textView.text[textView.text stringByAppendingFormat:@"城市:%@n",city];  
  97.         NSString* state[personaddress valueForKey:(NSString *)kABPersonAddressStateKey];  
  98.         if(state != nil)  
  99.             textView.text[textView.text stringByAppendingFormat:@"省:%@n",state];  
  100.         NSString* street[personaddress valueForKey:(NSString *)kABPersonAddressStreetKey];  
  101.         if(street != nil)  
  102.             textView.text[textView.text stringByAppendingFormat:@"街道:%@n",street];  
  103.         NSString* zip[personaddress valueForKey:(NSString *)kABPersonAddressZIPKey];  
  104.         if(zip != nil)  
  105.             textView.text[textView.text stringByAppendingFormat:@"邮编:%@n",zip];  
  106.         NSString* coutntrycode[personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey];  
  107.         if(coutntrycode != nil)  
  108.             textView.text[textView.text stringByAppendingFormat:@"国家编号:%@n",coutntrycode];  
  109.      
  110.       
  111.     //获取dates多值  
  112.     ABMultiValueRef datesABRecordCopyValue(person, kABPersonDateProperty);  
  113.     int datescountABMultiValueGetCount(dates);  
  114.     for (int y0<</span>datescounty++)  
  115.      
  116.         //获取dates Label  
  117.         NSString* datesLabel(NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));  
  118.         //获取dates值  
  119.         NSString* datesContent(NSString*)ABMultiValueCopyValueAtIndex(dates, y);  
  120.         textView.text[textView.text stringByAppendingFormat:@"%@:%@n",datesLabel,datesContent];  
  121.      
  122.     //获取kind值  
  123.     CFNumberRef recordTypeABRecordCopyValue(person, kABPersonKindProperty);  
  124.     if (recordType== kABPersonKindOrganization)  
  125.         // it's company  
  126.         NSLog(@"it's companyn");  
  127.     else  
  128.         // it's person, resource, or room  
  129.         NSLog(@"it's person, resource, or roomn");  
  130.      
  131.       
  132.       
  133.     //获取IM多值  
  134.     ABMultiValueRef instantMessageABRecordCopyValue(person, kABPersonInstantMessageProperty);  
  135.     for (int l1<</span>ABMultiValueGetCount(instantMessage); l++)  
  136.      
  137.         //获取IM Label  
  138.         NSString* instantMessageLabel(NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);  
  139.         textView.text[textView.text stringByAppendingFormat:@"%@n",instantMessageLabel];  
  140.         //获取該label下的2属性  
  141.         NSDictionary* instantMessageContent=(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);  
  142.         NSString* username[instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];  
  143.         if(username != nil)  
  144.             textView.text[textView.text stringByAppendingFormat:@"username:%@n",username];  
  145.           
  146.         NSString* service[instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];  
  147.         if(service != nil)  
  148.             textView.text[textView.text stringByAppendingFormat:@"service:%@n",service];  
  149.      
  150.       
  151.     //读取电话多值  
  152.     ABMultiValueRef phoneABRecordCopyValue(person, kABPersonPhoneProperty);  
  153.     for (int k0k<</span>ABMultiValueGetCount(phone); k++)  
  154.      
  155.         //获取电话Label  
  156.         NSString personPhoneLabel(NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));  
  157.         //获取該Label下的电话值  
  158.         NSString personPhone(NSString*)ABMultiValueCopyValueAtIndex(phone, k);  
  159.           
  160.         textView.text[textView.text stringByAppendingFormat:@"%@:%@n",personPhoneLabel,personPhone];  
  161.      
  162.       
  163.     //获取URL多值  
  164.     ABMultiValueRef urlABRecordCopyValue(person, kABPersonURLProperty);  
  165.     for (int m0<</span>ABMultiValueGetCount(url); m++)  
  166.      
  167.         //获取电话Label  
  168.         NSString urlLabel(NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));  
  169.         //获取該Label下的电话值  
  170.         NSString urlContent(NSString*)ABMultiValueCopyValueAtIndex(url,m);  
  171.           
  172.         textView.text[textView.text stringByAppendingFormat:@"%@:%@n",urlLabel,urlContent];  
  173.      
  174.       
  175.     //读取照片  
  176.     NSData *image(NSData*)ABPersonCopyImageData(person);  
  177.       
  178.       
  179.     UIImageView *myImage[[UIImageView alloc] initWithFrame:CGRectMake(200, 0, 50, 50)];  
  180.     [myImage setImage:[UIImage imageWithData:image]];  
  181.     myImage.opaqueYES 
  182.     [textView addSubview:myImage];  
  183.       
  184.       
  185.       
  186.  
  187.   
  188. CFRelease(results);  
  189. CFRelease(addressBook);  

AddressBook 代码详解相关推荐

  1. 【CV】Pytorch一小时入门教程-代码详解

    目录 一.关键部分代码分解 1.定义网络 2.损失函数(代价函数) 3.更新权值 二.训练完整的分类器 1.数据处理 2. 训练模型(代码详解) CPU训练 GPU训练 CPU版本与GPU版本代码区别 ...

  2. html5代码转换为视频,HTML5中的视频代码详解

    摘要 腾兴网为您分享:HTML5中的视频代码详解,智学网,云闪付,易推广,小红书等软件知识,以及360win10,流量魔盒,fitbit,上港商城,安卓2.3.7,全民惠,五年级下册英语单词表图片,t ...

  3. js php base64,JavaScript实现Base64编码与解码的代码详解

    本篇文章给大家分享的是jJavaScript实现Base64编码与解码的代码详解,内容挺不错的,希望可以帮助到有需要的朋友 一.加密解密方法使用//1.加密 var str = '124中文内容'; ...

  4. yii mysql 事务处理_Yii2中事务的使用实例代码详解

    前言 一般我们做业务逻辑,都不会仅仅关联一个数据表,所以,会面临事务问题. 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全 ...

  5. 代码详解|tensorflow实现 聊天AI--PigPig养成记(1)

    Chapter1.代码详解 完整代码github链接,Untitled.ipynb文件内. [里面的测试是还没训练完的时候测试的,今晚会更新训练完成后的测试结果] 修复了网上一些代码的bug,解决了由 ...

  6. vue build text html,Vue中v-text / v-HTML使用实例代码详解_放手_前端开发者

    废话少说,代码如下所述: /p> 显示123 /p> 补充:vuejs {{}},v-text 和 v-html的区别 {{message}} let app = new Vue({ el ...

  7. sift计算描述子代码详解_代码详解——如何计算横向误差?

    在路径跟踪控制的论文中,我们常会看到判断精确性的指标,即横向误差和航向误差,那么横向误差和航向误差如何获得? 在前几期代码详解中,参考路径和实际轨迹均由To Workspace模块导出,如图所示: 那 ...

  8. 委托与事件代码详解与(Object sender,EventArgs e)详解

    委托与事件代码详解 using System; using System.Collections.Generic; using System.Text; namespace @Delegate //自 ...

  9. python怎么画条形图-python绘制条形图方法代码详解

    1.首先要绘制一个简单的条形图 import numpy as np import matplotlib.pyplot as plt from matplotlib import mlab from ...

  10. python代码大全表解释-python操作列表的函数使用代码详解

    python的列表很重要,学习到后面你会发现使用的地方真的太多了.最近在写一些小项目时经常用到列表,有时其中的方法还会忘哎! 所以为了复习写下了这篇博客,大家也可以来学习一下,应该比较全面和详细了 列 ...

最新文章

  1. mimo的误码率_揭晓MU-MIMO黑科技!
  2. Java开发人员最常犯的10个错误,你犯过几个?
  3. [LOJ#2270][BZOJ4912][SDOI2017]天才黑客
  4. Java JTable3
  5. MySQL 十大常用字符串函数
  6. 群晖 root_群晖洗白简单教程
  7. 深度学习图像标注工具
  8. Python刚刚尝试就遇:SyntaxError: invalid syntax
  9. [算法]直线与圆的交点程序设计
  10. 专升本高等数学考试知识点汇总(一)
  11. WIFI共享大师无法开启发射功能
  12. Adobe Photoshop(Ps)2023软件安装包下载及安装教程(mac+windows多版PS软件) 超级丰富的!
  13. 区块链公司BitFury与联合国合作开展哈萨克斯坦的森林项目
  14. 混合硬盘计算机,什么是混合硬盘 什么是hhd硬盘?
  15. 第五人格服务器正在维护中怎么办,第五人格新联动刚来就出问题,紧急停服维护,这得补偿多少?...
  16. 金山词霸的字典引擎接口
  17. wordcloud出错_我在安装wordcloud时出错
  18. 数字信号处理实践方法 第二版 笔记
  19. 迅雷7.9.8.4550 Ayu精简绿化版
  20. HDU4218 IMBA?

热门文章

  1. 建行网银登录密码被盗,然后遭遇电话诈骗
  2. 服务器主板型号命令,Linux通过命令查询服务器型号、主板、CPU、内存及硬盘信息...
  3. java 跳跃表_你真的了解跳跃表吗
  4. socket长连接的维持
  5. 支付宝 java 签名_支付宝APP支付(Java后台生成签名具体步骤)
  6. linux中syscmd用法,M4 宏处理器
  7. python里的百分号_python里百分号什么意思
  8. 什么是IDOC,以及IDOC的步骤_小七_新浪博客
  9. cocos2dx[2.x](9)--编辑框之一CCTextFieldTTF
  10. cv2批量修改图片大小