[转]iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
一、在工程中添加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技巧之获取本机通讯录中的内容,解析通讯录源代码的更多相关文章
- javascript怎么获取指定url网页中的内容
javascript怎么获取指定url网页中的内容 一.总结 一句话总结:推荐jquery中ajax,简单方便. 1.js能跨域操作么? javascript出于安全机制不允许跨域操作的. 二.用ph ...
- IOS 开发之--获取真机的deviceToeken
获取真机的devicetoken的方法: #pragma mark 注册APNs成功并上报DeviceToken - (void)application:(UIApplication *)applic ...
- linux shell 脚本获取和替换文件中特定内容
1.从一串字符串中获取特定的信息 要求1:获取本机IP:menu.lst为系统镜象的IP配置文件,需要从中获取到本机IP信息(从文件获取信息) timeout title live find --se ...
- win32 获取 HotKey 控件中的内容(HKM_GETHOTKEY)
windows给我们提供了一个对话框控件HotKey非常好用,在设置热键的时候用起来很爽,但是一直百度就是没找到在win32下怎样通过消息获取这个控件里面的内容,找到的都是用MFC封装好的控件类来操作 ...
- IOS 开发之 -- 获取本机通讯录里面所有的联系人,并传到后台
项目中遇到一个需求,就是需要在入口的时候,获取通讯录的权限,并把所有的联系人,以接口参数的形式传到后台,通过网上查资料,历时3个小时,终于完成, 话不多,直接上代码: 1,导入系统库 #import ...
- [iOS]技巧集锦:UITableView自定义Cell中的控件无法完全对齐Cell的左边界和右边界
这是个很诡异的问题,由于一些特殊需求,我的TableView的Cell的背景色是透明,其中的控件会有背景色,第一个控件和最后一个控件我都用IB自动设了约束,对齐Cell的左边界和右边界,但是自动约束很 ...
- 正则获取 某段 DIV 中 的内容
string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div> ...
- 获取HttpServletRequest请求Body中的内容
在实际开发过程中,经常需要从 HttpServletRequest 中读取HTTP请求的body内容,俗话说的好”好记性不如烂笔头“,特在此将其读取方法记录一下. import java.io.Buf ...
- 以字符串形式获取excel单元格中的内容
public static String getCellValue(XSSFCell cell) { if (cell == null) { return ""; } switch ...
随机推荐
- phpcmsv9如何实现添加栏目时不在首页内容区显示只在导航栏显示
之前王晟璟一直使用PHPCMSV9系统建过自己的个人门户网站,同时也建立了一个其他类型的网站,感觉非常不错,我不得不说PHPCMSV9的功能非常齐全,非常强大. 但有一点时常让王晟璟感到很烦脑,那就是 ...
- C# Color Table颜色对照表
.AliceBlue 240,248,255 .LightSalmon 255,160,122 .AntiqueWhite 250,235,215 .LightSeaGreen 32,178,170 ...
- HackerRank "The Indian Job"
A sly knapsack problem in disguise! Thanks to https://github.com/bhajunsingh/programming-challanges/ ...
- 折腾Ipython
1. 用easy_install安装吧 [root@host python]# easy_install IPython Searching for IPython Reading https://p ...
- Python 通过print_lol将数据保存到文件中
1. 定义一个print_lol函数来控制列表的缩进和写入位置 import sys """this is a new fuction, which work for a ...
- android学习笔记41——图形图像处理1
图像图像处理 ImageView:用于显示普通静态图片: AnimationDrawable:用于开发逐帧动画: Animation:用于对普通图片使用补间动画: Bitmap.BitmapFacto ...
- POI按照源单元格设置目标单元格格式
原文:http://jjw198874.blog.163.com/blog/static/1889845522011102401854234/ POI按照源单元格设置目标单元格格式 poi按照一个源单 ...
- 【VB技巧】VB ListView 控件功能使用详解
来源:http://lcx.cc/?i=494 ListView控件 在工具箱上击鼠标右键,选择快捷菜单的Components(部件)项,在控件列表中选择Microsoft Windows Commo ...
- Ubuntu防火墙 UFW 设置
Ubuntu防火墙 UFW 设置 1.安装 sudo apt-get install ufw 2.启用 sudo ufw enable sudo ufw default deny 运行以上两条命令后, ...
- HDU 1011 Starship Troopers 树形DP 有坑点
本来是一道很水的树形DP题 设dp[i][j]表示,带着j个人去攻打以节点i为根的子树的最大收益 结果wa了一整晚 原因: 坑点1: 即使这个节点里面没有守卫,你如果想获得这个节点的收益,你还是必须派 ...