在不彈出通訊錄控制器的情況下獲取所有的通訊錄信息
1.設(shè)置 info.plist 的Privacy - Contacts Usage Description
2.導(dǎo)入頭文件
#import <Contacts/Contacts.h>
3.通過代碼獲取
//請(qǐng)求通訊錄權(quán)限
#pragma mark 請(qǐng)求通訊錄權(quán)限
- (void)requestContactAuthorAfterSystemVersion9{
CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusNotDetermined) {
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError* _Nullable error) {
if (error) {
NSLog(@"授權(quán)失敗");
}else {
NSLog(@"成功授權(quán)");
}
}];
}
else if(status == CNAuthorizationStatusRestricted)
{
NSLog(@"用戶拒絕");
[self showAlertViewAboutNotAuthorAccessContact];
}
else if (status == CNAuthorizationStatusDenied)
{
NSLog(@"用戶拒絕");
[self showAlertViewAboutNotAuthorAccessContact];
}
else if (status == CNAuthorizationStatusAuthorized)//已經(jīng)授權(quán)
{
//有通訊錄權(quán)限-- 進(jìn)行下一步操作
[self openContact];
}
}
//有通訊錄權(quán)限-- 進(jìn)行下一步操作
- (void)openContact{
// 獲取指定的字段,并不是要獲取所有字段夕土,需要指定具體的字段
NSArray *keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch];
CNContactStore *contactStore = [[CNContactStore alloc] init];
[contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
NSLog(@"-------------------------------------------------------");
NSString *givenName = contact.givenName;
NSString *familyName = contact.familyName;
NSLog(@"givenName=%@, familyName=%@", givenName, familyName);
//拼接姓名
NSString *nameStr = [NSString stringWithFormat:@"%@%@",contact.familyName,contact.givenName];
NSArray *phoneNumbers = contact.phoneNumbers;
// CNPhoneNumber * cnphoneNumber = contact.phoneNumbers[0];
// NSString * phoneNumber = cnphoneNumber.stringValue;
for (CNLabeledValue *labelValue in phoneNumbers) {
//遍歷一個(gè)人名下的多個(gè)電話號(hào)碼
NSString *label = labelValue.label;
// NSString * phoneNumber = labelValue.value;
CNPhoneNumber *phoneNumber = labelValue.value;
NSString * string = phoneNumber.stringValue ;
//去掉電話中的特殊字符
string = [string stringByReplacingOccurrencesOfString:@"+86" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@"(" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"姓名=%@, 電話號(hào)碼是=%@", nameStr, string);
}
// *stop = YES; // 停止循環(huán),相當(dāng)于break;
}];
}
//提示沒有通訊錄權(quán)限
- (void)showAlertViewAboutNotAuthorAccessContact{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:@"請(qǐng)授權(quán)通訊錄權(quán)限"
message:@"請(qǐng)?jiān)趇Phone的\"設(shè)置-隱私-通訊錄\"選項(xiàng)中,允許花解解訪問你的通訊錄"
preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:OKAction];
[self presentViewController:alertController animated:YES completion:nil];
}