頭文件 授權 關於通訊錄的授權這裡不再多說了,我在上一篇中有提到:通訊錄授權 訪問通訊錄 跳轉到系統通訊錄 添加代理 代理方法 註:上面兩個選擇回調代理實現一個就可以 相關借鑒:http://www.jb51.net/article/95500.htm ...
頭文件
#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