iOS:通訊錄(18-01-17更),ios通訊錄18-01-17
1、讀取通訊錄
1)、9.0以前
2)、9.0以後
2、調用通訊錄UI
1)、9.0以前
2)、9.0以後
3、參考
0、寫在前面
plist 需要設定隱私許可權
Privacy - Contacts Usage Description : 請求訪問通訊錄(自訂)
1、讀取通訊錄
1)、9.0以前
1-1)、標頭檔
#import <AddressBook/AddressBook.h>
1-2)、判斷是否有許可權
- (void)DetermineAndReadAddressBook{ // 判斷是否授權 ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus(); if (authorizationStatus == kABAuthorizationStatusNotDetermined) { // 請求授權 ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL); ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error){ if (granted) { // 授權成功 [self readAddressBook]; } else { // 授權失敗 NSLog(@"提示:使用者取消授權,讀取失敗"); } }); } else if (authorizationStatus == kABAuthorizationStatusAuthorized){ // 授權過 [self readAddressBook]; } else { dispatch_async(dispatch_get_main_queue(), ^{ // 更新介面 NSLog(@"提示:應用-通訊錄 設定"); }); }}
1-3)、讀取並儲存模型(未做)
- (void)readAddressBook { // 擷取所有連絡人 ABAddressBookRef addressBookRef = ABAddressBookCreate(); // 擷取所有連絡人 資料 CFArrayRef peoples = ABAddressBookCopyArrayOfAllPeople(addressBookRef); // 擷取所有連絡人 個數 CFIndex peoplesCount = ABAddressBookGetPersonCount(addressBookRef); for (int i = 0; i < peoplesCount; i++) { //擷取連絡人對象的引用 ABRecordRef people = CFArrayGetValueAtIndex(peoples, i); //擷取當前連絡人名字 NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty)); //擷取當前連絡人姓氏 NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty)); NSLog(@"--------------------------------------------------"); NSLog(@"firstName=%@, lastName=%@", firstName, lastName); //擷取當前連絡人中間名 NSString *middleName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNameProperty)); //擷取當前連絡人的名字首碼 NSString *prefix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonPrefixProperty)); //擷取當前連絡人的名字尾碼 NSString *suffix=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonSuffixProperty)); //擷取當前連絡人的暱稱 NSString *nickName=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNicknameProperty)); //擷取當前連絡人的名字拼音 NSString *firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty)); //擷取當前連絡人的姓氏拼音 NSString *lastNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonLastNamePhoneticProperty)); //擷取當前連絡人的中間名拼音 NSString *middleNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonMiddleNamePhoneticProperty)); //擷取當前連絡人的公司 NSString *organization=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonOrganizationProperty)); //擷取當前連絡人的職位 NSString *job=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonJobTitleProperty)); //擷取當前連絡人的部門 NSString *department=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonDepartmentProperty)); //擷取當前連絡人的生日 NSDate *birthday=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonBirthdayProperty)); //擷取當前連絡人的備忘 NSString *notes=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty)); //擷取當前連絡人頭像圖片 NSData *userImage=(__bridge NSData*)(ABPersonCopyImageData(people)); //擷取kind值 CFNumberRef kindType = ABRecordCopyValue(people, kABPersonKindProperty); if (kindType == kABPersonKindOrganization) { NSLog(@"公司"); } else { // it's a person, resource, or room NSLog(@"個人"); } //擷取建立當前連絡人的時間 注意是NSDate NSDate *creatTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonCreationDateProperty)); //擷取最近修改當前連絡人的時間 NSDate *alterTime=(__bridge NSDate*)(ABRecordCopyValue(people, kABPersonModificationDateProperty)); //擷取當前連絡人的電話 數組 NSMutableArray *phoneArray = [[NSMutableArray alloc]init]; ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty); CFIndex phonesCount = ABMultiValueGetCount(phones); for (NSInteger j=0; j<phonesCount; j++) { //擷取電話Label NSString *phoneLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phones, j)); //擷取該Label下的電話值 NSString *phone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j)); NSLog(@"phone=%@", phone); [phoneArray addObject:phone]; } //擷取IM多值 NSMutableArray *instantMessageArray = [[NSMutableArray alloc]init]; ABMultiValueRef instantMessages = ABRecordCopyValue(people, kABPersonInstantMessageProperty); CFIndex instantMessagesCount = ABMultiValueGetCount(instantMessages); for (int j = 1; j < instantMessagesCount; j++) { //擷取IM Label NSString* instantMessageLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(instantMessages, j); //擷取IM 的內容 NSDictionary* instantMessageContent =(__bridge NSDictionary*)ABMultiValueCopyValueAtIndex(instantMessages, j); NSString* username = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageUsernameKey]; NSString* service = [instantMessageContent valueForKey:(NSString *)kABPersonInstantMessageServiceKey]; } //擷取URL多值 NSMutableArray *urlArray = [[NSMutableArray alloc]init]; ABMultiValueRef urls = ABRecordCopyValue(people, kABPersonURLProperty); CFIndex urlsCount = ABMultiValueGetCount(urls); for (int j = 0; j < urlsCount; j++) { //擷取電話Label NSString * urlLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(urls, j)); //擷取該Label下的電話值 NSString * urlContent = (__bridge NSString*)ABMultiValueCopyValueAtIndex(urls,j); } //擷取當前連絡人的郵箱 注意是數組 NSMutableArray *emailArray = [[NSMutableArray alloc]init]; ABMultiValueRef emails= ABRecordCopyValue(people, kABPersonEmailProperty); CFIndex emailsCount = ABMultiValueGetCount(emails); for (NSInteger j=0; j< emailsCount; j++) { //擷取email Label NSString* emailLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emails, j)); //擷取email值 NSString *email = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(emails, j)); NSLog(@"email=%@", email); [emailArray addObject:email]; } //擷取地址 注意是數組 NSMutableArray *addressArray = [[NSMutableArray alloc]init]; ABMultiValueRef addresss = ABRecordCopyValue(people, kABPersonAddressProperty); CFIndex addresssCount = ABMultiValueGetCount(addresss); for (int j=0; j<addresssCount; j++) { // 地址類型 NSString *addressLabel = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(addresss, j)); NSDictionary * personaddress = (__bridge NSDictionary *)(ABMultiValueCopyValueAtIndex(addresss, j)); // 擷取地址 NSString* country = [personaddress valueForKey:(NSString *)kABPersonAddressCountryKey]; NSString* state = [personaddress valueForKey:(NSString *)kABPersonAddressStateKey]; NSString* city = [personaddress valueForKey:(NSString *)kABPersonAddressCityKey]; NSString* street = [personaddress valueForKey:(NSString *)kABPersonAddressStreetKey]; NSString* zip = [personaddress valueForKey:(NSString *)kABPersonAddressZIPKey]; NSString* coutntrycode = [personaddress valueForKey:(NSString *)kABPersonAddressCountryCodeKey]; //地址字串,可以按需求格式化 NSString *adress = [NSString stringWithFormat:@"國家:%@\n省:%@\n市:%@\n街道:%@\n郵編:%@",country,state,city,street,zip]; } //擷取當前連絡人紀念日 NSMutableArray *dateArr = [[NSMutableArray alloc]init]; ABMultiValueRef dates= ABRecordCopyValue(people, kABPersonDateProperty); CFIndex datesCount = ABMultiValueGetCount(dates); for (NSInteger j=0; j<datesCount; j++) { //擷取dates Label NSString* dateLabel = (__bridge NSString*)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(dates, j)); //擷取紀念日日期 NSDate *date =(__bridge NSDate*)(ABMultiValueCopyValueAtIndex(dates, j)); //擷取紀念日名稱 NSString *str =(__bridge NSString*)(ABMultiValueCopyLabelAtIndex(dates, j)); NSDictionary *tempDic = [NSDictionary dictionaryWithObject:date forKey:str]; [dateArr addObject:tempDic]; } }}
3、參考
《iOS的通訊錄開發》 --千煌89 簡書
《iOS 擷取通訊錄的4種方式詳解》 --vbirdbest CSDN