iOS7,iOS8和iOS9获取通讯录联系人

这篇文章主要介绍在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;

}



iOS 9 通讯录所用方法

授权:

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];

    

    //参数封装查询请求

    

    [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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风过不留痕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值