一、在工程中添加AddressBook.framework和AddressBookUI.framework

二、获取通讯录

1、在infterface中定义数组并在init方法中初始化

1
2
3
4
5
6
NSMutableArray *addressBookTemp;
 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    addressBookTemp = [NSMutableArray array];
}

2、定义一个model,用来存放通讯录中的各个属性

新建一个继承自NSObject的类,在.h中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@interface TKAddressBook : NSObject {
    NSInteger sectionNumber;
    NSInteger recordID;
    NSString *name;
    NSString *email;
    NSString *tel;
}
@property NSInteger sectionNumber;
@property NSInteger recordID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *email;
@property (nonatomic, retain) NSString *tel;
 
@end

在.m文件中进行synthesize

1
2
3
4
@implementation TKAddressBook
@synthesize name, email, tel, recordID, sectionNumber;
 
@end

3、获取联系人

在iOS6之后,获取通讯录需要获得权限

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
    //新建一个通讯录类
    ABAddressBookRef addressBooks = nil;
 
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
 
    {
        addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);
 
        //获取通讯录权限
 
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
 
        ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
 
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
 
        dispatch_release(sema);
 
    }
 
    else
 
    {
        addressBooks = ABAddressBookCreate();
 
    }
 
//获取通讯录中的所有人
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBooks);
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//通讯录中人数
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
 
//循环,获取每个人的个人信息
for (NSInteger i = 0; i < nPeople; i++)
    {
        //新建一个addressBook model类
        TKAddressBook *addressBook = [[TKAddressBook alloc] init];
        //获取个人
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        //获取个人名字
        CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
        CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
        CFStringRef abFullName = ABRecordCopyCompositeName(person);
        NSString *nameString = (__bridge NSString *)abName;
        NSString *lastNameString = (__bridge NSString *)abLastName;
         
        if ((__bridge id)abFullName != nil) {
            nameString = (__bridge NSString *)abFullName;
        } else {
            if ((__bridge id)abLastName != nil)
            {
                nameString = [NSString stringWithFormat:@"%@ %@", nameString, lastNameString];
            }
        }
        addressBook.name = nameString;
        addressBook.recordID = (int)ABRecordGetRecordID(person);;
         
        ABPropertyID multiProperties[] = {
            kABPersonPhoneProperty,
            kABPersonEmailProperty
        };
        NSInteger multiPropertiesTotal = sizeof(multiProperties) / sizeof(ABPropertyID);
        for (NSInteger j = 0; j < multiPropertiesTotal; j++) {
            ABPropertyID property = multiProperties[j];
            ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
            NSInteger valuesCount = 0;
            if (valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
             
            if (valuesCount == 0) {
                CFRelease(valuesRef);
                continue;
            }
            //获取电话号码和email
            for (NSInteger k = 0; k < valuesCount; k++) {
                CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
                switch (j) {
                    case 0: {// Phone number
                        addressBook.tel = (__bridge NSString*)value;
                        break;
                    }
                    case 1: {// Email
                        addressBook.email = (__bridge NSString*)value;
                        break;
                    }
                }
                CFRelease(value);
            }
            CFRelease(valuesRef);
        }
        //将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息
        [addressBookTemp addObject:addressBook];
         
        if (abName) CFRelease(abName);
        if (abLastName) CFRelease(abLastName);
        if (abFullName) CFRelease(abFullName);
    }

三、显示在table中

1
2
3
4
5
6
7
8
9
//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
 
//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [addressBookTemp count];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     
    NSString *cellIdentifier = @"ContactCell";
     
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
     
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
 
    TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];
 
    cell.textLabel.text = book.name;
 
    cell.detailTextLabel.text = book.tel;
 
    return cell;
}

列表效果

PS:通讯录中电话号码中的"-"可以在存入数组之前进行处理,属于NSString处理的范畴,解决办法有很多种,本文不多加说明

 
 

[转]iOS技巧之获取本机通讯录中的内容,解析通讯录源代码的更多相关文章

  1. javascript怎么获取指定url网页中的内容

    javascript怎么获取指定url网页中的内容 一.总结 一句话总结:推荐jquery中ajax,简单方便. 1.js能跨域操作么? javascript出于安全机制不允许跨域操作的. 二.用ph ...

  2. IOS 开发之--获取真机的deviceToeken

    获取真机的devicetoken的方法: #pragma mark 注册APNs成功并上报DeviceToken - (void)application:(UIApplication *)applic ...

  3. linux shell 脚本获取和替换文件中特定内容

    1.从一串字符串中获取特定的信息 要求1:获取本机IP:menu.lst为系统镜象的IP配置文件,需要从中获取到本机IP信息(从文件获取信息) timeout title live find --se ...

  4. win32 获取 HotKey 控件中的内容(HKM_GETHOTKEY)

    windows给我们提供了一个对话框控件HotKey非常好用,在设置热键的时候用起来很爽,但是一直百度就是没找到在win32下怎样通过消息获取这个控件里面的内容,找到的都是用MFC封装好的控件类来操作 ...

  5. IOS 开发之 -- 获取本机通讯录里面所有的联系人,并传到后台

    项目中遇到一个需求,就是需要在入口的时候,获取通讯录的权限,并把所有的联系人,以接口参数的形式传到后台,通过网上查资料,历时3个小时,终于完成, 话不多,直接上代码: 1,导入系统库 #import ...

  6. [iOS]技巧集锦:UITableView自定义Cell中的控件无法完全对齐Cell的左边界和右边界

    这是个很诡异的问题,由于一些特殊需求,我的TableView的Cell的背景色是透明,其中的控件会有背景色,第一个控件和最后一个控件我都用IB自动设了约束,对齐Cell的左边界和右边界,但是自动约束很 ...

  7. 正则获取 某段 DIV 中 的内容

    string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div> ...

  8. 获取HttpServletRequest请求Body中的内容

    在实际开发过程中,经常需要从 HttpServletRequest 中读取HTTP请求的body内容,俗话说的好”好记性不如烂笔头“,特在此将其读取方法记录一下. import java.io.Buf ...

  9. 以字符串形式获取excel单元格中的内容

    public static String getCellValue(XSSFCell cell) { if (cell == null) { return ""; } switch ...

随机推荐

  1. ORACLE的表被 另一个用户锁定,如何解除..

    SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$session s W ...

  2. php 添加redis扩展(二)

    php代码操作redis 1.连接 <?php //连接本地的 Redis 服务 $redis = new Redis(); $redis->connect('127.0.0.1', 63 ...

  3. lucene.net 使用过程中的 几个注意事项(含termquery 和QueryParser 的区别)

    几个注意事项1.建立索引时 插入的顺序(不设置document和字段的boost) 会影响到 查询结果的默认排序,建议:将最新生成的文章 最后建索引 这样 查询结果首先显示的是 最后插入的数据2.Bo ...

  4. 【Flex学习】Flex4学习网站

    http://blog.minidx.com/category/flex  来自为知笔记(Wiz)

  5. Learning Puppet — Resource Ordering

    Learning Puppet — Resource Ordering Learn about dependencies and refresh events, manage the relation ...

  6. c#启动进程

    //System.Diagnostics.Process p = new System.Diagnostics.Process(); //p.StartInfo.FileName = @"C ...

  7. postgresql数据库文件目录

    不同的发行版位置不同 查看进程 ps auxw | grep postgres | grep -- -D 找到默认的目录 /usr/lib/postgresql/9.4/bin/postgres -D ...

  8. Nginx实现内参:为什么架构很重要?

    Nginx在web开发者眼中就是高并发高性能的代名词,其基于事件的架构也被众多开发者效仿.我从Nginx的网站找到一篇技术文章将Nginx是怎样实现的,文章是Nginx的产品老大Owen Garret ...

  9. Redis简单使用方法说明

    安装 www.redis.io下载安装包tar zxvf redis.tar.gzcd redismakecd src && make install移动文件,便于管理:mkdir - ...

  10. python函数参数前面单星号(*)和双星号(**)的区别

    在python的函数中经常能看到输入的参数前面有一个或者两个星号:例如 def foo(param1, *param2): def bar(param1, **param2): 这两种用法其实都是用来 ...