获取通讯录
来源:互联网 发布:apm2.8源码 编辑:程序博客网 时间:2024/05/06 06:59
一、在工程中添加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];
}
新建一个继承自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
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);
}
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;
}
0 0
- 获取通讯录
- 获取通讯录
- 获取通讯录
- 获取通讯录
- 获取通讯录
- 获取通讯录
- 获取通讯录
- 获取通讯录好友邮箱!
- 获取手机通讯录
- 获取iPhone通讯录信息
- 获取iPhone通讯录信息
- 获取iPhone通讯录信息
- 获取iPhone通讯录信息
- 获取iphone通讯录~~
- iphone获取通讯录
- android获取通讯录信息
- 获取通讯录所有字段
- 获取通讯录所有字段
- 建立链表和打印链表简单代码
- 一个可以将usb摄像头的视频和麦克风采集的声音以RTMP协议发布到RTMP Server的应用程序
- Hibernate持久化对象的三个状态
- UVA 10375 - Choose and divide【唯一分解定理】
- J-- DZY Loves Sequences(CF-447C
- 获取通讯录
- HDOJ M斐波那契数列 4549【矩阵快速幂+快速幂+费马小定理+欧拉函数】
- 搜索引擎深度抓取和广度抓取如何理解
- 黑马程序员---反射
- IOS OC 多态(白话)
- Unity 3D学习笔记(二)—— 脚本01
- [HDU 4004] The Frog's Games 二分+贪心
- 区间dp入门[POJ2955][HDU2476][POJ3186][POJ1651][HDU4632][HDU4283][HDU4570][POJ3280]
- 贪心算法之ian Ji -- The Horse Racing