ios获取系统通讯录方法

网上相关贴子很多,但基本上都来自同一个出处,基础不好的同学可能看来起会很吃力,我今天整理了一下,化繁为简只提取通讯录中姓名与电话号码,给出最简便的方便供大家参考

为方便大家调用,我写成了返回数组的方法,直接copy到你的代码中即可取得联系人对象的数组,废话不多说直接上代码吧,数据模型可自定义,只要包含姓名、号码两个成员变量即可,我这里就不粘了,根据需要自定义吧,如有需要可回复

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- (NSArray *)getContacts:(ABAddressBookRef)adress
{
    // 1、获取通讯录
    CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(adress);
 
    // 2、遍历通讯录
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < CFArrayGetCount(results); i++){
 
        // 2.1、获取单条记录
        ABRecordRef person = CFArrayGetValueAtIndex(results, i);
 
        // 2.2、取记录中的名字
        NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSString *name = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
        if (firstName) name = [name stringByAppendingString:firstName];                     // 名字拼接
 
        // 2.3、取记录中的号码
        ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);         // 所有号码
        for (int k = 0; k < ABMultiValueGetCount(phones); k++){                             // 遍历取值
            NSString * phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, k); // 取出号码
            RMContact *contact = [RMContact contactWithName:name phone:phone];
            [array addObject:contact];                                                      // 加入数组
        }
    }
    return array;
}


其中:RMContact *contact = [RMContact contactWithName:name phone:phone];为自定义模型,根据需求自己去实现。
方便Copy写成代码块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
- (NSArray *)getContacts:(ABAddressBookRef)adress
{
????// 1、获取通讯录
????CFArrayRef results = ABAddressBookCopyArrayOfAllPeople(adress);
?????
????// 2、遍历通讯录
????NSMutableArray *array = [NSMutableArray array];
????for (int i = 0; i < CFArrayGetCount(results); i++){
?????????
????????// 2.1、获取单条记录
????????ABRecordRef person = CFArrayGetValueAtIndex(results, i);
?????????
????????// 2.2、取记录中的名字
????????NSString *firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
????????NSString *name = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
????????if (firstName) name = [name stringByAppendingString:firstName];???????????????????? // 名字拼接
?????????
????????// 2.3、取记录中的号码
????????ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);???????? // 所有号码
????????for (int k = 0; k < ABMultiValueGetCount(phones); k++){???????????????????????????? // 遍历取值
????????????NSString * phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, k); // 取出号码
????????????RMLocalContact *contact = [RMLocalContact contactWithName:name phone:phone];
????????????[array addObject:contact];????????????????????????????????????????????????????? // 加入数组
????????}
????}
????_contacts = [NSArray arrayWithArray:array];
?????
????return array;
}

Demo:Demo

相当链接:ios开发获取手机通讯录(最简单明了)

Leave a Reply