IOS通讯录编程 获取通讯录信息

一、通讯录编程首先在项目中导入库文件AddressBook.framework、AddressBookUI.framework

二、在相应的类中导入头文件

#import  
#import 

三、直接上代码

//读取所有联系人
-(void)ReadAllPeoples
{
    [[NetworkActivityViewController shareInstance] show:self msg:@"正在获取..."];
    //这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录
    int __block tip=0;
    //声明一个通讯簿的引用
    ABAddressBookRef addBook =nil;
    //因为在IOS6.0之后和之前的权限申请方式有所差别,这里做个判断
    if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
        //创建通讯簿的引用
        addBook=ABAddressBookCreateWithOptions(NULL, NULL);
        //创建一个出事信号量为0的信号
        dispatch_semaphore_t sema=dispatch_semaphore_create(0);
        //申请访问权限
        ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error)        {
            //greanted为YES是表示用户允许,否则为不允许
            if (!greanted) {
                tip=1;
            }
            //发送一次信号
            dispatch_semaphore_signal(sema);
        });
        //等待信号触发
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }else{
        //IOS6之前
        addBook =ABAddressBookCreate();
    }
    if (tip) {
        //做一个友好的提示
        UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的通讯录\n设置>通用>访问限制" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alart show];
        return;
    }

    //获取所有联系人的数组
    CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook);
    //获取联系人总数
    CFIndex number = ABAddressBookGetPersonCount(addBook);

    //进行遍历
    for (NSInteger i=0; i//获取联系人对象的引用
        ABRecordRef  people = CFArrayGetValueAtIndex(allLinkPeople, i);
        //获取当前联系人名字
        NSString *firstName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty));
        //获取当前联系人姓氏
        NSString *lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty));
        if (lastName==nil) {
            lastName=@"";
        }
        if (firstName==nil) {
            firstName=@"";
        }
        model.phoneName=[NSString stringWithFormat:@"%@%@",lastName,firstName];

        //获取当前联系人的电话 数组
        NSMutableArray * phoneArr = [[NSMutableArray alloc]init];
        ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty);
        for (NSInteger j=0; jNSString *)(ABMultiValueCopyValueAtIndex(phones, j))];
            NSString *phone=(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j));
             NSLog(@"phone==%@",phone);

        }

    }

}

你可能感兴趣的:(IOS,ios,通讯录)