IOS

关注公众号 jb51net

关闭
首页 > 软件编程 > IOS > IOS 读取addressbook

IOS 开发之读取addressbook的实现实例

作者:chyroger

这篇文章主要介绍了IOS 开发之读取addressbook的实现实例的相关资料,希望通过本文大家能够掌握这样的内容,需要的朋友可以参考下

IOS 开发之读取addressbook的实现实例

iphone读取addressbook:

1.添加addressbook.framework

2.在需要的源文件中    

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

3.开始粘代码:

//get all people info from the address book 
ABAddressBookRef addressBook = ABAddressBookCreate(); 
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);//这是个数组的引用 
for(int i = 0; i<CFArrayGetCount(people); i++){ 
  //parse each person of addressbook 
  ABRecordRef record=CFArrayGetValueAtIndex(people, i);//取出一条记录 
  //以下的属性都是唯一的,即一个人只有一个FirstName,一个Organization。。。 
  CFStringRef firstName = ABRecordCopyValue(record,kABPersonFirstNameProperty); 
  CFStringRef lastName = ABRecordCopyValue(record,kABPersonLastNameProperty); 
  CFStringRef company = ABRecordCopyValue(record,kABPersonOrganizationProperty); 
  CFStringRef department = ABRecordCopyValue(record,kABPersonDepartmentProperty); 
  CFStringRef job = ABRecordCopyValue(record,kABPersonJobTitleProperty); 
  //"CFStringRef"这个类型也是个引用,可以转成NSString* 
  NSlog((NSString *)firstName); 
  //...... 
  //所有这些应用都是要释放的,手册里是说“you are responsible to release it" 
  (firstName==NULL)?:CFRelease(firstName); 
  (lastName==NULL)?:CFRelease(lastName); 
  (company==NULL)?:CFRelease(company); 
  (department==NULL)?:CFRelease(department); 
  (job==NULL)?:CFRelease(job); 
  //....... 
  //有些属性不是唯一的,比如一个人有多个电话:手机,主电话,传真。。。 
  ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); 
  //所有ABMutableMultiValueRef这样的引用的东西都是这样一个元组(id,label,value) 
  multiPhone = ABRecordCopyValue(record, kABPersonPhoneProperty); 
  for (CFIndex i = 0; i < ABMultiValueGetCount(multiPhone); i++) { 
    CFStringRef labelRef = ABMultiValueCopyLabelAtIndex(multiPhone, i); 
    CFStringRef numberRef = ABMultiValueCopyValueAtIndex(multiPhone, i); 
    //可以通过元组的label来判定这个电话是哪种电话,比如下面就包括:主电话,手机,工作传真 
    if([(NSString *)labelRef isEqualToString:(NSString *) kABPersonPhoneMainLabel]){ 
      person._mainPhone = (NSString *)numberRef; 
    }else if([(NSString *)labelRef isEqualToString:(NSString *) kABPersonPhoneMobileLabel]){ 
      person._cellPhone = (NSString *)numberRef; 
    }else if([(NSString *)labelRef compare:(NSString *) kABPersonPhoneWorkFAXLabel]==NSOrderedSame){ 
      person._fax = (NSString *)numberRef; 
    } 
    CFRelease(labelRef); 
    CFRelease(numberRef); 
  } 
  CFRelease(multiPhone); 
} 
//释放资源 
//其他还有url,email,地址等等属性都是ABMutableMultiValueRef多值类型的,可以采用循环来遍历 
 

完了,保存addressbook的部分等以后。

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
阅读全文