iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
来源:互联网 发布:婚庆后期软件 编辑:程序博客网 时间:2024/05/16 05:00
一、在工程中添加AddressBook.framework和AddressBookUI.framework
二、获取通讯录
1、在infterface中定义数组并在init方法中初始化
1
NSMutableArray *addressBookTemp;
2
3
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
4
{
5
addressBookTemp = [NSMutableArray array];
6
}
新建一个继承自NSObject的类,在.h中
01
@interface TKAddressBook : NSObject {
02
NSInteger sectionNumber;
03
NSInteger recordID;
04
NSString *name;
05
NSString *email;
06
NSString *tel;
07
}
08
@property NSInteger sectionNumber;
09
@property NSInteger recordID;
10
@property (nonatomic, retain) NSString *name;
11
@property (nonatomic, retain) NSString *email;
12
@property (nonatomic, retain) NSString *tel;
13
14
@end
1
@implementation TKAddressBook
2
@synthesize name, email, tel, recordID, sectionNumber;
3
4
@end
01
//新建一个通讯录类
02
ABAddressBookRef addressBooks = ABAddressBookCreate();
03
CFArrayRef allPeople =
04
05
//获取通讯录中的所有人
06
ABAddressBookCopyArrayOfAllPeople(addressBooks);
07
08
//通讯录中人数
09
CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
10
11
//循环,获取每个人的个人信息
12
for
(NSInteger i = 0; i < nPeople; i++)
13
{
14
//新建一个addressBook model类
15
TKAddressBook *addressBook = [[TKAddressBook alloc] init];
16
//获取个人
17
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
18
//获取个人名字
19
CFTypeRef abName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
20
CFTypeRef abLastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
21
CFStringRef abFullName = ABRecordCopyCompositeName(person);
22
NSString *nameString = (__bridge NSString *)abName;
23
NSString *lastNameString = (__bridge NSString *)abLastName;
24
25
if
((__bridge id)abFullName != nil) {
26
nameString = (__bridge NSString *)abFullName;
27
}
else
{
28
if
((__bridge id)abLastName != nil)
29
{
30
nameString = [NSString stringWithFormat:@
"%@ %@"
, nameString, lastNameString];
31
}
32
}
33
addressBook.name = nameString;
34
addressBook.recordID = (
int
)ABRecordGetRecordID(person);;
35
36
ABPropertyID multiProperties[] = {
37
kABPersonPhoneProperty,
38
kABPersonEmailProperty
39
};
40
NSInteger multiPropertiesTotal =
sizeof
(multiProperties) /
sizeof
(ABPropertyID);
41
for
(NSInteger j = 0; j < multiPropertiesTotal; j++) {
42
ABPropertyID property = multiProperties[j];
43
ABMultiValueRef valuesRef = ABRecordCopyValue(person, property);
44
NSInteger valuesCount = 0;
45
if
(valuesRef != nil) valuesCount = ABMultiValueGetCount(valuesRef);
46
47
if
(valuesCount == 0) {
48
CFRelease(valuesRef);
49
continue
;
50
}
51
//获取电话号码和email
52
for
(NSInteger k = 0; k < valuesCount; k++) {
53
CFTypeRef value = ABMultiValueCopyValueAtIndex(valuesRef, k);
54
switch
(j) {
55
case
0: {
// Phone number
56
addressBook.tel = (__bridge NSString*)value;
57
break
;
58
}
59
case
1: {
// Email
60
addressBook.email = (__bridge NSString*)value;
61
break
;
62
}
63
}
64
CFRelease(value);
65
}
66
CFRelease(valuesRef);
67
}
68
//将个人信息添加到数组中,循环完成后addressBookTemp中包含所有联系人的信息
69
[addressBookTemp addObject:addressBook];
70
71
if
(abName) CFRelease(abName);
72
if
(abLastName) CFRelease(abLastName);
73
if
(abFullName) CFRelease(abFullName);
74
}
1
//行数
2
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
3
return
1;
4
}
5
6
//列数
7
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
8
return
[addressBookTemp count];
9
}
01
//cell内容
02
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
03
04
NSString *cellIdentifier = @
"ContactCell"
;
05
06
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
07
08
if
(cell == nil){
09
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
10
}
11
12
TKAddressBook *book = [addressBookTemp objectAtIndex:indexPath.row];
13
14
cell.textLabel.text = book.name;
15
16
cell.detailTextLabel.text = book.tel;
17
18
return
cell;
19
}
0 0
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- ios获取本机通讯录中的内容,解析通讯录源代码
- 获取本机通讯录中的内容,解析通讯录源代码
- iOS技巧之获取本机通讯录中的内容
- iOS技巧之获取本机通讯录中的内容…
- iOS之获取手机通讯录
- 内容解析者获取手机通讯录信息
- iOS 手机通讯录,将电话保存到本机通讯录
- iOS开发之模仿qq通讯录源代码!
- 安卓之内容提供者获取通讯录
- IOS通讯录内容调用
- IOS学习之获取iphone通讯录
- iOS初级开发之获取通讯录
- 如何快速编译hal层的修改
- SLF4J 的几种实际应用模式--之三:JCL-Over-SLF4J+SLF4J
- ORACLE 常用命令
- Knight Moves uva
- 算法java实现--贪心算法--多机调度问题
- iOS技巧之获取本机通讯录中的内容,解析通讯录源代码
- WinCE 进程、线程和内存管理之同步
- web-polygraph源码框架和session处理流程笔记
- hdu3336(kmp)
- maven scope runtime
- POJ Shortest Prefixes
- centos cacti 安装.
- 多线程入门教程
- cocos2dx项目如何集成shareSDK分享