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

同时引入两个文件

    #import <AddressBook/AddressBook.h>  #import <AddressBookUI/AddressBookUI.h>  

读取手机通讯录
ABAddressBookRef addressBook = ABAddressBookCreate();

读取联系人 小明
CFStringRef cfName = CFSTR("小明");
NSArray *people = (NSArray*)ABAddressBookCopyPeopleWithName(myAddressBook, cfName);

people就是名字为小明的联系人数组。默认对象是CFArray,取长度方法为:CFArrayGetCount(people)
为了方便强制转换成了NSArray(现在不用强制转换默认也是NSArray)

其中一个小明

    if(people != nil && [people count]>0){  ABRecordRef aXiaoming0 = CFArrayGetValueAtIndex(people,0);  }  //获取小明0的名字  CFStringRef cfname = ABRecordCopyValue(aXiaoming0, kABPersonFirstNameProperty);  //获取小明0的电话信息  ABMultiValueRef cfphone = ABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty);  //获取小明0的第0个电话类型:(比如 工作,住宅,iphone,移动电话等)  CFStringRef leixin = ABMultiValueCopyLabelAtIndex(cfphone,0);  //获取小明0的第3个电话号码:(使用前先判断长度ABMultiValueGetCount(cfphone)>4)  CFStringRef haoma = ABMultiValueCopyValueAtIndex(cfphone,3);  //添加一个联系人  CFErrorRef anError = NULL;  ABRecordRef aContact = ABPersonCreate();//联系人  //名字  NSString* name = @"小利";  CFStringRef cfsname = CFStringCreateWithCString( kCFAllocatorDefault, [name UTF8String], kCFStringEncodingUTF8);  ABRecordSetValue(aContact, kABPersonFirstNameProperty, cfsname, &anError);//写入名字进联系人  //号码  ABMultiValueRef phone =ABMultiValueCreateMutable(kABMultiStringPropertyType);  ABMultiValueAddValueAndLabel(phone, @“13800138000”,kABPersonPhoneMobileLabel, NULL);//添加移动号码0  ABMultiValueAddValueAndLabel(phone, @“18688888888”,kABPersonPhoneIPhoneLabel, 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 =ABAddressBookCopyArrayOfAllPeople(addressBook);  for (id person in (NSArray *)allperson) {  }  

添加联系人

 //name  ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();  ABRecordRef newPerson = ABPersonCreate();  CFErrorRef error = NULL;  ABRecordSetValue(newPerson, kABPersonFirstNameProperty, firsrName.text, &error);  ABRecordSetValue(newPerson, kABPersonLastNameProperty, lastName.text, &error);  ABRecordSetValue(newPerson, kABPersonOrganizationProperty, company.text, &error);  ABRecordSetValue(newPerson, kABPersonFirstNamePhoneticProperty, firsrNamePY.text, &error);  ABRecordSetValue(newPerson, kABPersonLastNamePhoneticProperty, lastNamePY.text, &error);  //phone number  ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);  ABMultiValueAddValueAndLabel(multiPhone, houseNumber.text, kABPersonPhoneHomeFAXLabel, NULL);  ABMultiValueAddValueAndLabel(multiPhone, mobileNumber.text, kABPersonPhoneMobileLabel, NULL);  ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone, &error);  CFRelease(multiPhone);  //email  ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);  ABMultiValueAddValueAndLabel(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 = ABAddressBookGetPersonWithRecordID(iPhoneAddressBook, recordID);  if (!oldPeople) {  return;  }  ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();  ABAddressBookRemoveRecord(iPhoneAddressBook, oldPeople, &error);  ABAddressBookSave(iPhoneAddressBook, &error);  CFRelease(iPhoneAddressBook);  CFRelease(oldPeople);  

获取所有组

    CFArrayRef array = ABAddressBookCopyArrayOfAllGroups(iPhoneAddressBook);  for (id group in (NSArray *)array) {  NSLog(@"group name = %@", ABRecordCopyValue(group, kABGroupNameProperty));  NSLog(@"group id = %d", ABRecordGetRecordID(group));  }  

删除组

    ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();  ABRecordRef oldGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook, RecordID);  ABAddressBookRemoveRecord(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 = ABAddressBookCopyArrayOfAllPeople(addressBook);  for(int i = 0; i < CFArrayGetCount(results); i++)  {  ABRecordRef person = CFArrayGetValueAtIndex(results, i);  //读取firstname  NSString *personName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);  if(personName != nil)  textView.text = [textView.text stringByAppendingFormat:@"\n姓名:%@\n",personName];  //读取lastname  NSString *lastname = (NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);  if(lastname != nil)  textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastname];  //读取middlename  NSString *middlename = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNameProperty);  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, kABPersonNicknameProperty);  if(nickname != nil)  textView.text = [textView.text stringByAppendingFormat:@"%@\n",nickname];  //读取firstname拼音音标  NSString *firstnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty);  if(firstnamePhonetic != nil)  textView.text = [textView.text stringByAppendingFormat:@"%@\n",firstnamePhonetic];  //读取lastname拼音音标  NSString *lastnamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty);  if(lastnamePhonetic != nil)  textView.text = [textView.text stringByAppendingFormat:@"%@\n",lastnamePhonetic];  //读取middlename拼音音标  NSString *middlenamePhonetic = (NSString*)ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty);  if(middlenamePhonetic != nil)  textView.text = [textView.text stringByAppendingFormat:@"%@\n",middlenamePhonetic];  //读取organization公司  NSString *organization = (NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);  if(organization != nil)  textView.text = [textView.text stringByAppendingFormat:@"%@\n",organization];  //读取jobtitle工作  NSString *jobtitle = (NSString*)ABRecordCopyValue(person, kABPersonJobTitleProperty);  if(jobtitle != nil)  textView.text = [textView.text stringByAppendingFormat:@"%@\n",jobtitle];  //读取department部门  NSString *department = (NSString*)ABRecordCopyValue(person, kABPersonDepartmentProperty);  if(department != nil)  textView.text = [textView.text stringByAppendingFormat:@"%@\n",department];  //读取birthday生日  NSDate *birthday = (NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);  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, kABPersonCreationDateProperty);  NSLog(@"第一次添加该条记录的时间%@\n",firstknow);  //最后一次修改該条记录的时间  NSString *lastknow = (NSString*)ABRecordCopyValue(person, kABPersonModificationDateProperty);  NSLog(@"最后一次修改該条记录的时间%@\n",lastknow);  //获取email多值  ABMultiValueRef email = ABRecordCopyValue(person, kABPersonEmailProperty);  int emailcount = ABMultiValueGetCount(email);  for (int x = 0; x < emailcount; x++)  {  //获取email Label  NSString* emailLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(email, x));  //获取email值  NSString* emailContent = (NSString*)ABMultiValueCopyValueAtIndex(email, x);  textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",emailLabel,emailContent];  }  //读取地址多值  ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);  int count = ABMultiValueGetCount(address);  for(int j = 0; j < count; j++)  {  //获取地址Label  NSString* addressLabel = (NSString*)ABMultiValueCopyLabelAtIndex(address, j);  textView.text = [textView.text stringByAppendingFormat:@"%@\n",addressLabel];  //获取該label下的地址6属性  NSDictionary* personaddress =(NSDictionary*) ABMultiValueCopyValueAtIndex(address, j);  NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey];  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 *)kABPersonAddressStreetKey];  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 *)kABPersonAddressCountryCodeKey];  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 < datescount; y++)  {  //获取dates Label  NSString* datesLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, y));  //获取dates值  NSString* datesContent = (NSString*)ABMultiValueCopyValueAtIndex(dates, y);  textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",datesLabel,datesContent];  }  //获取kind值  CFNumberRef recordType = ABRecordCopyValue(person, kABPersonKindProperty);  if (recordType == kABPersonKindOrganization) {  // it's a company  NSLog(@"it's a company\n");  } else {  // it's a person, resource, or room  NSLog(@"it's a person, resource, or room\n");  }  //获取IM多值  ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);  for (int l = 1; l < ABMultiValueGetCount(instantMessage); l++)  {  //获取IM Label  NSString* instantMessageLabel = (NSString*)ABMultiValueCopyLabelAtIndex(instantMessage, l);  textView.text = [textView.text stringByAppendingFormat:@"%@\n",instantMessageLabel];  //获取該label下的2属性  NSDictionary* instantMessageContent =(NSDictionary*) ABMultiValueCopyValueAtIndex(instantMessage, l);  NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey];  if(username != nil)  textView.text = [textView.text stringByAppendingFormat:@"username:%@\n",username];  NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey];  if(service != nil)  textView.text = [textView.text stringByAppendingFormat:@"service:%@\n",service];  }  //读取电话多值  ABMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);  for (int k = 0; k<ABMultiValueGetCount(phone); k++)  {  //获取电话Label  NSString * personPhoneLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phone, k));  //获取該Label下的电话值  NSString * personPhone = (NSString*)ABMultiValueCopyValueAtIndex(phone, k);  textView.text = [textView.text stringByAppendingFormat:@"%@:%@\n",personPhoneLabel,personPhone];  }  //获取URL多值  ABMultiValueRef url = ABRecordCopyValue(person, kABPersonURLProperty);  for (int m = 0; m < ABMultiValueGetCount(url); m++)  {  //获取电话Label  NSString * urlLabel = (NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(url, m));  //获取該Label下的电话值  NSString * urlContent = (NSString*)ABMultiValueCopyValueAtIndex(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 相关操作小计相关推荐

  1. uni map地图相关使用小计(多点标识,拖拽起点,地图画圆)

    这次的项目用到了地图,特别记录下 小程序不执行地图相关事件时候,看下小程序的调试基础库 进行多点标注时候,经纬度要正确,图标设置50就好了,ID是一定得绑定的. 动态传参时候如果需要保留之前的标注点就 ...

  2. linux 学习操作小计

    屌丝最近在接触lamp开发 把工作中遇到的 问题和 一些常用的操作记下来.以便以后去翻阅 (1)linux下备份mysql数据库方法 #mysqldump -u root -p dbname > ...

  3. Python基础教程:list相关操作

    list相关操作小例子 获取list的下标和值 >>> mylist = ['a', 'b', 'c', 'd'] >>> for index, value in ...

  4. 微信小程序实现微信支付的相关操作设置

    本文不涉及相关API的实现,旨在记录实现微信支付需要在微信公众平台和微信支付的商户平台需要进行的操作. 1.首先需要用户申请了微信小程序和入驻微信商户平台 2.获取小程序的appid  设置AppSe ...

  5. 销售额总和的分类汇总计算机操作,销售数据excel表格分类-如何在EXCEL表格中按类别小计?...

    如何在EXCEL表格中按类别小计? EXCEL中把数据分类步骤如下: 1.在产品销售情况表中,完成对各产品销售额总和的分类汇总,汇总结果显示在数据下方. 2.选择数据--分类汇总. 3.得到分类汇总对 ...

  6. jQuery实现PC端商城购物车模块基本功能(每个商品的小计和合计都会根据添加和删除的操作来动态计算)

    jQuery实现PC端商城购物车模块基本功能 先上效果图: 因为主要是想练习jQuery的使用,所以页面CSS部分比较简陋,有需要的话,大家在参考代码时,可以自己再完善下CSS部分的代码,让购物车页面 ...

  7. 基于Javaweb的小项目(类似于qqzone) 8 —— 回复相关操作

    功能描述 展示回复:点击日志列表中的某个日志标题,在展示该日志的详情信息下面,需要展示该日志的相关回复 添加回复:只需要输入回复内容 删除回复:在对应的回复中,鼠标经过该回复,则会出现删除图标,点击可 ...

  8. 库,表,记录的相关操作

    系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等 performance_schema: MySQ ...

  9. excel每页小计累计_您还需要Excel小计吗?

    excel每页小计累计 Excel has a SUBTOTAL function, which ignores hidden or filtered rows. There is a Subtota ...

  10. excel函数:汉字转全拼_星期五的Excel函数:带过滤器的小计和总和

    excel函数:汉字转全拼 Last week, we used the Excel SUBTOTAL function to sum items in a filtered list, while ...

最新文章

  1. BF,KMP,BM三种字符串匹配算法性能比较
  2. Oracle Enterprise Linux
  3. ThreadPoolExecutor的七个参数详解
  4. linux中kafka主题修改分区,kafka_2.11-2.0.0的部署与配置修改
  5. 苹果与阿拉斯加航空公司合作 采用iPad Pro进行自助值机登机
  6. mongoDB之find()
  7. 和python高级知识_Python中的5个高阶概念属性的知识点!你要了解明白哦!
  8. 短时间让大家对C++ STL有所学习
  9. Unity3D脚印6——模型动画
  10. java静态类堆栈_Java回归学习-面向对象内存分析-堆栈
  11. linux 检查文件更新,Linux系统文件系统优化及磁盘检查
  12. ETF最神秘的地带:清算交收(背景知识篇)
  13. TASKCTL-作业属性总体概述
  14. 有软件测试台式电脑电源供电不足吗,电脑主机等电源供电不足的症状
  15. 8个高质量免抠素材网站
  16. Python 基础 1加到100 求和
  17. 计算机系统如何恢复出厂设置路由器,斐讯p.to怎么恢复出厂设置?-斐讯路由器设置...
  18. 激活win10专业版,桌面设置我的电脑,测试过可行
  19. html5字体统一解决方案
  20. 微信小程序隐藏左上角返回首页按钮

热门文章

  1. 阿里云部署视频点播:学习文档使用Java程序部署视频点播基础用法之入门
  2. vue省市区三级联动(地区编码)
  3. Windows下搭建论坛服务器
  4. 哪些深度相机有python接口_三种主流深度相机介绍
  5. GAN-cls:具有匹配感知的判别器
  6. Linux第六章课后题
  7. Docker网桥模式ping不通宿主机
  8. 宇宙飞船大战html5游戏,GitHub - WildDogTeam/demo-js-starwars: 基于WildDog的太空大战游戏...
  9. -TEST 20 for NOIP 。。。(80-300)-----(( ! ))
  10. pyecharts学习笔记