ios獲取手機系統(tǒng)通訊錄

獲取通訊錄大體分兩大種、四小種,兩大種分別是基于address框架和contacts框架開發(fā)的,四小種就是這兩大種分別的有UI和無UI。

但是address框架在iOS9之后就被棄用了,雖然還可以用,但是畢竟掌握新技術才是正道。下面我就把這幾種的代碼貼上來,供大家參考。

一、使用AddressBook.framework框架

包含框架

#import<AddressBook/AddressBook.h>

#import<AddressBookUI/AddressBookUI.h>

集成代理 ABPeoplePickerNavigationControllerDelegate

1、使用UI界面

- (IBAction)addressYes:(id)sender {

ABPeoplePickerNavigationController * peoplePickerNav = [ABPeoplePickerNavigationController new];

peoplePickerNav.peoplePickerDelegate=self;

[selfpresentViewController:peoplePickerNav animated:YEScompletion:nil];

}

//===========address//- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person {////? ? NSLog(@"選中了person,%@",person);//}

-(void)peoplePickerNavigationController(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{

NSLog(@"選中了屬性,property:%d,identifier:%d",property,identifier);

}

2、界面無UI

// address框架無UI

- (IBAction)addressNo:(id)sender {

//這個變量用于記錄授權是否成功,即用戶是否允許我們訪問通訊錄

int__block tip =0;

//聲明一個通訊簿的引用

ABAddressBookRef addBook =nil;

//創(chuàng)建通訊簿的引用

addBook = ABAddressBookCreateWithOptions(NULL,NULL);

//創(chuàng)建一個出事信號量為0的信號

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

//申請訪問權限

ABAddressBookRequestAccessWithCompletion(addBook, ^(boolgreanted, CFErrorRef error)? ? ? ? {

//greanted為YES是表示用戶允許,否則為不允許

if(!greanted) {

tip =1;

}

//發(fā)送一次信號

dispatch_semaphore_signal(sema);

});

//等待信號觸發(fā)

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);if(tip) {

//做一個友好的提示

UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"溫馨提示"message:@"請您設置允許APP訪問您的通訊錄\nSettings>General>Privacy"delegate:selfcancelButtonTitle:@"確定"otherButtonTitles:nil,nil];

[alart show];return;

}

//獲取所有聯(lián)系人的數(shù)組

CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);

//獲取聯(lián)系人總數(shù)

CFIndex number = ABAddressBookGetPersonCount(addBook);

//進行遍歷

for(inti =0; i < number; i++) {

//獲取聯(lián)系人對象的引用

ABRecordRef? people = CFArrayGetValueAtIndex(allLinkPeople, i);

//獲取當前聯(lián)系人名字

NSString* firstName = (__bridgeNSString*)(ABRecordCopyValue(people, kABPersonFirstNameProperty));

//獲取當前聯(lián)系人姓氏

NSString* lastName=(__bridgeNSString*)(ABRecordCopyValue(people, kABPersonLastNameProperty));

//獲取當前聯(lián)系人的名字拼音

NSString* firstNamePhoneic=(__bridgeNSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty));

//獲取當前聯(lián)系人的備注

NSString* notes = (__bridgeNSString*)(ABRecordCopyValue(people, kABPersonNoteProperty));

//獲取當前聯(lián)系人的電話 數(shù)組

NSMutableArray* phoneArr = [[NSMutableArrayalloc]init];

ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty);for(NSIntegerj =0; j < ABMultiValueGetCount(phones); j++) {

[phoneArr addObject:(__bridgeNSString*)(ABMultiValueCopyValueAtIndex(phones, j))];

}

//獲取當前聯(lián)系人頭像圖片

NSData * userImage=(__bridge NSData*)(ABPersonCopyImageData(people));NSLog(@"firstName:%@,lastName:%@,firstNamePhoneic:%@,notes:%@,phoneArr:%@,userImage:%@",firstName,lastName,firstNamePhoneic,notes,phoneArr,userImage);

}

}

二、Contacts框架

包含框架

#import<Contacts/Contacts.h>

#import<ContactsUI/ContactsUI.h>

集成代理 CNContactPickerDelegate

代碼如下? TKAddressBook *addressBook為數(shù)據(jù)模型,self.listContacts為承載模型的可變數(shù)組。

[self.listContacts removeAllObjects];

CNContactStore *store = [[CNContactStore alloc] init];

NSArray *typeArray = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey,CNContactOrganizationNameKey,CNContactEmailAddressesKey,CNContactPostalAddressesKey,CNContactInstantMessageAddressesKey];

CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:typeArray];

//遍歷所有的聯(lián)系人

NSMutableDictionary *addressBookDict = [NSMutableDictionary dictionary];

[store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {

NSArray *phones = contact.phoneNumbers;

for (int i = 0; i < phones.count; i++) {

TKAddressBook *addressBook = [[TKAddressBook alloc] init];

NSString *firstName = contact.givenName;

NSString *lastName = contact.familyName;

if ([Tool isBlankString:firstName]) {

firstName = @"";

}

if ([Tool isBlankString:lastName]) {

lastName = @"";

}

//姓名

addressBook.name = [NSString stringWithFormat:@"%@%@",lastName,firstName];

CNLabeledValue *phone = phones[i];

CNPhoneNumber *phoneNum = phone.value;

NSString *num = phoneNum.stringValue;

num = [num stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

num = [num stringByReplacingOccurrencesOfString:@"\r" withString:@""];

num = [num stringByReplacingOccurrencesOfString:@"\n" withString:@""];

num = [num stringByReplacingOccurrencesOfString:@"\t" withString:@""];

num = [num stringByReplacingOccurrencesOfString:@"+86" withString:@""];

num = [num stringByReplacingOccurrencesOfString:@"-" withString:@""];

num = [num stringByReplacingOccurrencesOfString:@" " withString:@""];

addressBook.mobileNo = [NSString stringWithFormat:@"%@",num];

//公司

addressBook.company = contact.organizationName;

NSMutableString *str = [[NSMutableString alloc]init];;

//電子郵箱

for (int i = 0; i < contact.emailAddresses.count; i++) {

CNLabeledValue *email = contact.emailAddresses[i];

if (i == 0) {

[str appendString:[NSString stringWithFormat:@"%@",email.value]];

}else if(i > 0 &&i < contact.emailAddresses.count - 1){

[str appendString:[NSString stringWithFormat:@",%@",email.value]];

}

}

addressBook.emailArr = str;

//地址

NSString *addressStr = [[NSString alloc]init];

for (int i = 0; i < contact.postalAddresses.count; i++) {

CNLabeledValue *address = contact.postalAddresses[0];

CNPostalAddress *model = address.value;

addressStr = [NSString stringWithFormat:@"%@%@%@%@%@",model.country,model.state,model.city,model.subLocality,model.street];

}

addressStr = [addressStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和換行字符

addressStr = [addressStr stringByReplacingOccurrencesOfString:@"\r" withString:@""];

addressStr = [addressStr stringByReplacingOccurrencesOfString:@"\n" withString:@""];

addressStr = [addressStr stringByReplacingOccurrencesOfString:@"\t" withString:@""];

addressBook.address = addressStr;

[self.listContacts addObject:addressBook];

NSString *firstLetterString = [self getFirstLetterFromString:addressBook.name];

if (addressBookDict[firstLetterString]){

[addressBookDict[firstLetterString] addObject:addressBook];

}else{

//創(chuàng)建新發(fā)可變數(shù)組存儲該首字母對應的聯(lián)系人模型

NSMutableArray *arrGroupNames = [NSMutableArray arrayWithObject:addressBook];

//將首字母-姓名數(shù)組作為key-value加入到字典中

[addressBookDict setObject:arrGroupNames forKey:firstLetterString];

}

self.tableDic = addressBookDict;

}

}];

TKAddressBook *addressBook模型參數(shù)如下

@property (strong, nonatomic) NSString *name;

@property (strong, nonatomic) NSString *emailArr;

@property (strong, nonatomic) NSString *mobileNo;

@property (strong, nonatomic) NSString *company;? ? //公司

@property (strong, nonatomic) NSString *address;

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容