您好,登錄后才能下訂單哦!
IOS 開發(fā)之讀取addressbook的實(shí)現(xiàn)實(shí)例
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);//這是個(gè)數(shù)組的引用 for(int i = 0; i<CFArrayGetCount(people); i++){ //parse each person of addressbook ABRecordRef record=CFArrayGetValueAtIndex(people, i);//取出一條記錄 //以下的屬性都是唯一的,即一個(gè)人只有一個(gè)FirstName,一個(gè)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"這個(gè)類型也是個(gè)引用,可以轉(zhuǎn)成NSString* NSlog((NSString *)firstName); //...... //所有這些應(yīng)用都是要釋放的,手冊(cè)里是說“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); //....... //有些屬性不是唯一的,比如一個(gè)人有多個(gè)電話:手機(jī),主電話,傳真。。。 ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); //所有ABMutableMultiValueRef這樣的引用的東西都是這樣一個(gè)元組(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來判定這個(gè)電話是哪種電話,比如下面就包括:主電話,手機(jī),工作傳真 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多值類型的,可以采用循環(huán)來遍歷
完了,保存addressbook的部分等以后。
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。