这篇文章主要介绍在iOS7 -iOS8.0和iOS9.0无界面获取通讯录信息的方法.下面我们逐个介绍。
iOS7 - iOS8 无界面获取通讯录信息
首页要导入库:AddressBookUI 和 AddressBook
授权:
ABAuthorizationStatus status =ABAddressBookGetAuthorizationStatus();
switch (status) {
casekABAuthorizationStatusNotDetermined:
casekABAuthorizationStatusRestricted:
{
//参数1通讯录对象 参数2block 回调
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL, NULL);
//申请授权
ABAddressBookRequestAccessWithCompletion(book, ^(bool granted,CFErrorRef error) {
if (granted) {
NSLog(@"授权成功");
}else{
NSLog(@"授权失败");
}
});
}
break;
//系统关闭了该App通讯录权限
casekABAuthorizationStatusDenied:
{
NSLog(@"没有权限");
}
break;
casekABAuthorizationStatusAuthorized:
{
//已经授权过了,可以进行下一步操作
}
break;
default:
break;
}
获取通讯录信息
- (NSMutableArray *)getContacts
{
//获取联系人信息
NSMutableArray *phoneNumbers = [[NSMutableArrayalloc]init];
ABAddressBookRef book = ABAddressBookCreateWithOptions(NULL,NULL);
CFArrayRef allpeople = ABAddressBookCopyArrayOfAllPeople(book);
CFIndex count = CFArrayGetCount(allpeople);
for (CFIndex i =0; i <count ; i++) {
ABRecordRef record = CFArrayGetValueAtIndex(allpeople, i);
CFStringRef strFirst = ABRecordCopyValue(record,kABPersonFirstNameProperty);
CFStringRef strmdills = ABRecordCopyValue(record,kABPersonMiddleNameProperty);
CFStringRef strfamily = ABRecordCopyValue(record,kABPersonLastNameProperty);
NSString *str =[NSStringstringWithFormat:@"%@%@%@",(__bridge_transferNSString *)strfamily,(__bridge_transferNSString *)strmdills,(__bridge_transferNSString *)strFirst];
// NSLog(@"%@",str);
//电话号码
ABMultiValueRef multivalue = ABRecordCopyValue(record,kABPersonPhoneProperty);
NSMutableArray *phoneMut = [[NSMutableArrayalloc]init];
for (CFIndex i =0; i <ABMultiValueGetCount(multivalue); i++) {
CFStringRef phoneStr = ABMultiValueCopyValueAtIndex(multivalue, i);
// NSLog(@"phoneStr = %@",phoneStr);
NSString *phone = (__bridge_transfer NSString *)(phoneStr);
//该方法为去掉-()等符号方法
NSString *aamyphone = [ContactsformatPhoneNumber:phone];
//通讯录模型只包含了名字和电话号码
ContactPhoneModel *phone1 = [[ContactPhoneModelalloc]init];
phone1.name = str;
phone1.phone = aamyphone;
[phoneNumbers addObject:phone1];
// NSLog(@"格式化:%@",aamyphone);
// NSLog(@"%@",phone);
CFRelease(phoneStr);
}
CFRelease(strfamily);
CFRelease(strmdills);
CFRelease(strFirst);
}
CFRelease(allpeople);
return phoneNumbers;
}
//去除号码格式
+ (NSString *)formatPhoneNumber:(NSString*)number
{
number = [number stringByReplacingOccurrencesOfString:@"-"withString:@""];
number = [number stringByReplacingOccurrencesOfString:@" "withString:@""];
number = [number stringByReplacingOccurrencesOfString:@"("withString:@""];
number = [number stringByReplacingOccurrencesOfString:@")"withString:@""];
number = [number stringByReplacingOccurrencesOfString:@" "withString:@""];
NSInteger len = number.length;
if (len < 6)
{
return number;
}
if ([[numbersubstringToIndex:2]isEqualToString:@"86"])
{
number = [number substringFromIndex:2];
}
elseif ([[numbersubstringToIndex:3]isEqualToString:@"+86"])
{
number = [number substringFromIndex:3];
}
elseif ([[numbersubstringToIndex:4]isEqualToString:@"0086"])
{
number = [number substringFromIndex:4];
}
elseif ([[numbersubstringToIndex:5]isEqualToString:@"12593"])
{
number = [number substringFromIndex:5];
}
elseif ([[numbersubstringToIndex:5]isEqualToString:@"17951"])
{
number = [number substringFromIndex:5];
}
else if (len ==16 && [[numbersubstringToIndex:6]isEqualToString:@"125201"])
{
number = [number substringFromIndex:5];
}
return number;
}
CNAuthorizationStatus status = [CNContactStoreauthorizationStatusForEntityType:CNEntityTypeContacts];
//如果没有授权过需要请求用户的授权
CNContactStore *store = [[CNContactStorealloc]init];
cn.store = store;
switch (status) {
caseCNAuthorizationStatusNotDetermined:
caseCNAuthorizationStatusRestricted:
{
//请求授权
[store requestAccessForEntityType:CNEntityTypeContactscompletionHandler:^(BOOL granted,NSError *_Nullable error) {
if (granted) {
NSLog(@"授权成功");
}else{
NSLog(@"授权失败");
}];
}
break;
caseCNAuthorizationStatusDenied:
{
NSLog(@"没有权限");
}
break;
caseCNAuthorizationStatusAuthorized:
{
//已经授权可以进行下一步操作
}
break;
default:
break;
}
- (NSMutableArray *)ios9GetContacts
{
//name phone
//该方法包含了需要获取信息的类型,可根据自己的需要选择添加类型
CNContactFetchRequest *request = [[CNContactFetchRequestalloc]initWithKeysToFetch:@[CNContactGivenNameKey,CNContactPhoneNumbersKey,CNContactMiddleNameKey,CNContactFamilyNameKey]];
NSMutableArray *phoneNumbers = [[NSMutableArrayalloc]init];
//参数1 封装查询请求
[self.storeenumerateContactsWithFetchRequest: requesterror:nilusingBlock:^(CNContact *_Nonnull contact,BOOL *_Nonnull stop) {
for (CNLabeledValue * labeledValuein contact.phoneNumbers) {
CNPhoneNumber *num = labeledValue.value;
// NSLog(@"num = %@",num.stringValue);
//
// NSLog(@"num111 = %@",[num valueForKey:@"digits"]);
//联系人模型
ContactPhoneModel *phone = [[ContactPhoneModelalloc]init];
phone.name =[NSStringstringWithFormat:@"%@%@%@" ,contact.familyName,contact.middleName,contact.givenName];
phone.phone = [ContactsformatPhoneNumber:num.stringValue];
[phoneNumbers addObject:phone];
}
}];
return phoneNumbers;
}
项目源码:https://github.com/WorkerHuan/myContactpppp