之前項(xiàng)目中寫到通訊錄的東西敬锐,一直沒(méi)有時(shí)間記錄一下沐序,這不看到網(wǎng)上有人記錄了下來(lái)彬檀,在這里記錄一下。
1. iOS 9.0之前獲取通訊錄的方法
- (void)fetchAddressBookBeforeIOS9{
ABAddressBookRef addressBook = ABAddressBookCreate();
//首次訪問(wèn)需用戶授權(quán)
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {//首次訪問(wèn)通訊錄
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (!error) {
if (granted) {//允許
NSLog(@"已授權(quán)訪問(wèn)通訊錄");
NSArray *contacts = [self fetchContactWithAddressBook:addressBook];
dispatch_async(dispatch_get_main_queue(), ^{
//----------------主線程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}else{//拒絕
NSLog(@"拒絕訪問(wèn)通訊錄");
}
}else{
NSLog(@"發(fā)生錯(cuò)誤!");
}
});
}else{//非首次訪問(wèn)通訊錄
NSArray *contacts = [self fetchContactWithAddressBook:addressBook];
dispatch_async(dispatch_get_main_queue(), ^{
//----------------主線程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}
}
- (NSMutableArray *)fetchContactWithAddressBook:(ABAddressBookRef)addressBook{
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {////有權(quán)限訪問(wèn)
//獲取聯(lián)系人數(shù)組
NSArray *array = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSMutableArray *contacts = [NSMutableArray array];
for (int i = 0; i < array.count; i++) {
//獲取聯(lián)系人
ABRecordRef people = CFArrayGetValueAtIndex((__bridge ABRecordRef)array, i);
//獲取聯(lián)系人詳細(xì)信息,如:姓名,電話,住址等信息
NSString *firstName = (__bridge NSString *)ABRecordCopyValue(people, kABPersonFirstNameProperty);
NSString *lastName = (__bridge NSString *)ABRecordCopyValue(people, kABPersonLastNameProperty);
ABMutableMultiValueRef *phoneNumRef = ABRecordCopyValue(people, kABPersonPhoneProperty);
NSString *phoneNumber = ((__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneNumRef)).lastObject;
[contacts addObject:@{@"name": [firstName stringByAppendingString:lastName], @"phoneNumber": phoneNumber}];
}
return contacts;
}else{//無(wú)權(quán)限訪問(wèn)
NSLog(@"無(wú)權(quán)限訪問(wèn)通訊錄");
return nil;
}
}
打印結(jié)果:
2.iOS 9.0 及 iOS 9.0之后獲取通訊錄的方法
- (void)fetchAddressBookOnIOS9AndLater{
//創(chuàng)建CNContactStore對(duì)象
CNContactStore *contactStore = [[CNContactStore alloc] init];
//首次訪問(wèn)需用戶授權(quán)
if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) {//首次訪問(wèn)通訊錄
[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error){
if (granted) {//允許
NSLog(@"已授權(quán)訪問(wèn)通訊錄");
NSArray *contacts = [self fetchContactWithContactStore:contactStore];//訪問(wèn)通訊錄
dispatch_async(dispatch_get_main_queue(), ^{
//----------------主線程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}else{//拒絕
NSLog(@"拒絕訪問(wèn)通訊錄");
}
}else{
NSLog(@"發(fā)生錯(cuò)誤!");
}
}];
}else{//非首次訪問(wèn)通訊錄
NSArray *contacts = [self fetchContactWithContactStore:contactStore];//訪問(wèn)通訊錄
dispatch_async(dispatch_get_main_queue(), ^{
//----------------主線程 更新 UI-----------------
NSLog(@"contacts:%@", contacts);
});
}
}
- (NSMutableArray *)fetchContactWithContactStore:(CNContactStore *)contactStore{
//判斷訪問(wèn)權(quán)限
if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusAuthorized) {//有權(quán)限訪問(wèn)
NSError *error = nil;
//創(chuàng)建數(shù)組,必須遵守CNKeyDescriptor協(xié)議,放入相應(yīng)的字符串常量來(lái)獲取對(duì)應(yīng)的聯(lián)系人信息
NSArray <id<CNKeyDescriptor>> *keysToFetch = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey];
//獲取通訊錄數(shù)組
NSArray<CNContact*> *arr = [contactStore unifiedContactsMatchingPredicate:nil keysToFetch:keysToFetch error:&error];
if (!error) {
NSMutableArray *contacts = [NSMutableArray array];
for (int i = 0; i < arr.count; i++) {
CNContact *contact = arr[i];
NSString *givenName = contact.givenName;
NSString *familyName = contact.familyName;
NSString *phoneNumber = ((CNPhoneNumber *)(contact.phoneNumbers.lastObject.value)).stringValue;
[contacts addObject:@{@"name": [givenName stringByAppendingString:familyName], @"phoneNumber": phoneNumber}];
}
return contacts;
}else {
return nil;
}
}else{//無(wú)權(quán)限訪問(wèn)
NSLog(@"無(wú)權(quán)限訪問(wèn)通訊錄");
return nil;
}
}
打印結(jié)果:
項(xiàng)目中發(fā)現(xiàn)鱼喉,iOS9之前的獲取通訊錄,第一次系統(tǒng)會(huì)有提示打開趋观,而iOS9之后的扛禽,直接打開。拆内。