ios address book 操作大全

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

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

[html]  view plain copy
  1. #import <AddressBook/AddressBook.h>  
  2. #import <AddressBookUI/AddressBookUI.h>  
读取手机通讯录
ABAddressBookRef addressBook = ABAddressBookCreate();

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

people就是名字为小明的联系人数组。默认对象是CFArray,取长度方法为:CFArrayGetCountpeople
为了方便强制转换成了NSArray

其中一个小明

[html]  view plain copy
  1. if(people != nil && [people count]>0){  
  2.         ABRecordRef aXiaoming0 = CFArrayGetValueAtIndex(people,0);  
  3. }  
  4.   
  5. //获取小明0的名字  
  6. CFStringRef cfname = ABRecordCopyValue(aXiaoming0, kABPersonFirstNameProperty);  
  7.   
  8. //获取小明0的电话信息  
  9. ABMultiValueRef cfphone = ABRecordCopyValue(aXiaoming0, kABPersonPhoneProperty);  
  10.   
  11. //获取小明0的第0个电话类型:(比如 工作,住宅,iphone,移动电话等)  
  12. CFStringRef leixin = ABMultiValueCopyLabelAtIndex(cfphone,0);  
  13.   
  14. //获取小明0的第3个电话号码:(使用前先判断长度ABMultiValueGetCount(cfphone)>4)  
  15. CFStringRef haoma = ABMultiValueCopyValueAtIndex(cfphone,3);  
  16.   
  17. //添加一个联系人  
  18.   
  19. CFErrorRef anError = NULL;  
  20. ABRecordRef aContact = ABPersonCreate();//联系人  
  21.   
  22. //名字  
  23. NSString* name = @"小利";  
  24. CFStringRef cfsname = CFStringCreateWithCString( kCFAllocatorDefault, [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 plain copy
  1. CFArrayRef allperson =ABAddressBookCopyArrayOfAllPeople(addressBook);  
  2. for (id person in (NSArray *)allperson) {  
  3. }  

添加联系人

[html]  view plain copy
  1. //name  
  2.  ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();  
  3.  ABRecordRef newPerson = ABPersonCreate();  
  4.  CFErrorRef error = NULL;  
  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 multiPhone = ABMultiValueCreateMutable(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 multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);  
  18.  ABMultiValueAddValueAndLabel(multiEmail, email.text, kABWorkLabel, NULL);  
  19.  ABRecordSetValue(newPerson, kABPersonEmailProperty, multiEmail, &error);  
  20.  CFRelease(multiEmail);  
  21.  //picture  
  22.  NSData *dataRef = UIImagePNGRepresentation(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 plain copy
  1. CFErrorRef error = NULL;  
  2. ABRecordRef oldPeople = ABAddressBookGetPersonWithRecordID(iPhoneAddressBook, recordID);  
  3. if (!oldPeople) {  
  4.     return;  
  5. }  
  6. ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();  
  7. ABAddressBookRemoveRecord(iPhoneAddressBook, oldPeople, &error);  
  8. ABAddressBookSave(iPhoneAddressBook, &error);  
  9. CFRelease(iPhoneAddressBook);  
  10. CFRelease(oldPeople);  

获取所有组


[html]  view plain copy
  1. CFArrayRef array = ABAddressBookCopyArrayOfAllGroups(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 plain copy
  1. ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();  
  2. ABRecordRef oldGroup = ABAddressBookGetGroupWithRecordID(iPhoneAddressBook, RecordID);  
  3. ABAddressBookRemoveRecord(iPhoneAddressBook, oldGroup, nil);  
  4. ABAddressBookSave(iPhoneAddressBook, nil);  
  5. CFRelease(iPhoneAddressBook);  
  6. CFRelease(oldGroup);  


添加组

[html]  view plain copy
  1. ABAddressBookRef  iPhoneAddressBook = ABAddressBookCreate();  
  2. ABRecordRef  newGroup = ABGroupCreate();  
  3. ABRecordSetValue(newGroup, kABGroupNameProperty, groupName.text, nil);  
  4. ABAddressBookAddRecord(iPhoneAddressBook, newGroup, nil);  
  5. ABAddressBookSave(iPhoneAddressBook, nil);  
  6. CFRelease(newGroup);  
  7. CFRelease(iPhoneAddressBook);  

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

[html]  view plain copy
  1. ABAddressBookRef addressBook = ABAddressBookCreate();  
  2.  CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(addressBook);  
  3.  for(int i = 0; i < CFArrayGetCount(results); i++)  
  4.  {  
  5.      ABRecordRef person = CFArrayGetValueAtIndex(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 email = ABRecordCopyValue(person, kABPersonEmailProperty);  
  71.      int emailcount = ABMultiValueGetCount(email);  
  72.      for (int x = 0; x < emailcount; x++)  
  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 address = ABRecordCopyValue(person, kABPersonAddressProperty);  
  82.      int count = ABMultiValueGetCount(address);  
  83.        
  84.      for(int j = 0; j < count; j++)  
  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 dates = ABRecordCopyValue(person, kABPersonDateProperty);  
  113.      int datescount = ABMultiValueGetCount(dates);  
  114.      for (int y = 0; y < datescount; y++)  
  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 recordType = ABRecordCopyValue(person, kABPersonKindProperty);  
  124.      if (recordType == kABPersonKindOrganization) {  
  125.          // it's a company  
  126.          NSLog(@"it's a company\n");  
  127.      } else {  
  128.          // it's a person, resource, or room  
  129.          NSLog(@"it's a person, resource, or room\n");  
  130.      }  
  131.        
  132.        
  133.      //获取IM多值  
  134.      ABMultiValueRef instantMessage = ABRecordCopyValue(person, kABPersonInstantMessageProperty);  
  135.      for (int l = 1; l < 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 phone = ABRecordCopyValue(person, kABPersonPhoneProperty);  
  153.      for (int k = 0; k<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 url = ABRecordCopyValue(person, kABPersonURLProperty);  
  165.      for (int m = 0; m < 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.opaque = YES;  
  182.      [textView addSubview:myImage];  
  183.        
  184.        
  185.        
  186.  }  
  187.    
  188.  CFRelease(results);  
  189.  CFRelease(addressBook);  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值