欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-18 14:51:28
ios 开发之读取addressbook的实现实例 iphone读取addressbook: 1.添加addressbook.framework 2.在需要的...

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的部分等以后。

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

上一篇:

下一篇: