iOS-擷取通訊錄連絡人資訊,ios-通訊錄連絡人
標頭檔
#import <AddressBook/AddressBook.h>#import <AddressBookUI/AddressBookUI.h>
授權
關於通訊錄的授權這裡不再多說了,我在上一篇中有提到:通訊錄授權
訪問通訊錄
跳轉到系統通訊錄
- (void)jumpAddress{ ABPeoplePickerNavigationController *peoplePickVc = [[ABPeoplePickerNavigationController alloc] init]; peoplePickVc.peoplePickerDelegate = self; [self presentViewController:peoplePickVc animated:YES completion:nil];}
添加代理
@interface ViewController ()<ABPeoplePickerNavigationControllerDelegate, UINavigationControllerDelegate>
代理方法
///取消選擇- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker{ }
///實現此方法後,下面的代理方法不會實現(此方法是點擊通訊錄列表中名字直接調用並dismiss)- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person { //擷取當前連絡人名字 NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); //擷取當前連絡人姓氏 NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); //擷取當前連絡人的電話 數組 NSMutableArray *phoneArray = [[NSMutableArray alloc]init]; ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty); for (NSInteger j=0; j<ABMultiValueGetCount(phones); j++) { NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j)); NSLog(@"phone=%@", phone); [phoneArray addObject:phone]; } //擷取當前連絡人的郵箱 注意是數組 NSMutableArray *emailArray = [[NSMutableArray alloc]init]; ABMultiValueRef emails= ABRecordCopyValue(person, kABPersonEmailProperty); for (NSInteger j=0; j<ABMultiValueGetCount(emails); j++) { NSString *email = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j)); NSLog(@"email=%@", email); [emailArray addObject:email]; } //擷取當前連絡人中間名 NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonMiddleNameProperty)); //擷取當前連絡人的名字首碼 NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonPrefixProperty)); //擷取當前連絡人的名字尾碼 NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonSuffixProperty)); //擷取當前連絡人的暱稱 NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonNicknameProperty)); //擷取當前連絡人的名字拼音 NSString *firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonFirstNamePhoneticProperty)); //擷取當前連絡人的姓氏拼音 NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonLastNamePhoneticProperty)); //擷取當前連絡人的中間名拼音 NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonMiddleNamePhoneticProperty)); //擷取當前連絡人的公司 NSString *organization=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonOrganizationProperty)); //擷取當前連絡人的職位 NSString *job=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonJobTitleProperty)); //擷取當前連絡人的部門 NSString *department=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonDepartmentProperty)); //擷取當前連絡人的生日// NSString *birthday=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonBirthdayProperty)); //擷取當前連絡人的備忘 NSString *notes=(__bridge NSString*)(ABRecordCopyValue(person, kABPersonNoteProperty)); //擷取建立當前連絡人的時間 注意是NSDate NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonCreationDateProperty)); //擷取最近修改當前連絡人的時間 NSDate *alterTime=(__bridge NSDate*)(ABRecordCopyValue(person, kABPersonModificationDateProperty)); //擷取當前連絡人頭像圖片 NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(person)); //擷取當前連絡人紀念日 NSMutableArray *dateArr = [[NSMutableArray alloc]init]; ABMultiValueRef dates= ABRecordCopyValue(person, kABPersonDateProperty); for (NSInteger j=0; j<ABMultiValueGetCount(dates); j++) { //擷取紀念日日期 NSDate *data =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j)); //擷取紀念日名稱 NSString *str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j)); }}
///實現此方法後,上面的代理方法不會實現(此方法是進入通訊錄詳情後點擊名稱或電話等調用並dismiss)- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier NS_AVAILABLE_IOS(8_0){}
註:上面兩個選擇回調代理實現一個就可以
相關借鑒:http://www.jb51.net/article/95500.htm