您好,登錄后才能下訂單哦!
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的部分等以后。
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。