iOS查看手机通讯录

查看iphone的手机通讯录的话,需要用到一个库AddressBook。

可以使用里面的方法调用我们自己的通许录

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
-(void) getPhoneContacts{
ABAddressBookRef addressBook = nil;
if([[UIDevice currentDevice].systemVersion doubleValue] >= 6.0)
{
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
addressBook = ABAddressBookCreate();
}

NSArray *temPeoples = (__bridge NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
for(id temPerson in temPeoples)
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:2];
NSMutableArray *phoneArray = [[NSMutableArray alloc] initWithCapacity:3];

NSString *tmpFirstName = (__bridge NSString *) ABRecordCopyValue((__bridge ABRecordRef)(temPerson), kABPersonFirstNameProperty);
NSString *tmpLastName = (__bridge NSString *) ABRecordCopyValue((__bridge ABRecordRef)(temPerson), kABPersonLastNameProperty);

[dic setValue:[NSString stringWithFormat:@"%@ %@", tmpFirstName, tmpLastName] forKey:@"name"];
ABMultiValueRef phone = ABRecordCopyValue((__bridge ABRecordRef)(temPerson), kABPersonPhoneProperty);

for(int k = 0; k < ABMultiValueGetCount(phone); k++)
{
NSString *personPhone = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phone, k);
[phoneArray addObject:personPhone];
}

[dic setValue:phoneArray forKey:@"phone"];
[resultArray addObject:dic];
}
}

ABAddressBookCreate

ABAddressBookCreateWithOptions,ABAddressBookRequestAccessWithCompletion

是不同版本所使用的方法

ABAddressBookCreate适用于6.0以上的,另外的则使用雨6.0以后的,

之后调用ABAddressBookCopyArrayOfAllPeople,获取通讯录的内容。

代码如下:

PhoneContactsViewController.m

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// PhoneContactsViewController.m
// PhoneContacts
//
// Created by david on 13-9-25.
// Copyright (c) 2013年 WalkerFree. All rights reserved.
//

#import "PhoneContactsViewController.h"
#import <AddressBook/AddressBook.h>

@interface PhoneContactsViewController ()

@end

@implementation PhoneContactsViewController

@synthesize resultArray;
@synthesize tableView;

- (void)viewDidLoad
{
[super viewDidLoad];
// Dispose of any resources that can be recreated.
self.navigationItem.title = @"查看手机通讯录";
resultArray = [[NSMutableArray alloc] initWithCapacity:100];
[self getPhoneContacts];

//left的button
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setFrame:CGRectMake(0, 0, 25, 26)];
[btnBack setTitle:@"返回" forState:UIControlStateNormal];
[btnBack addTarget:self action:@selector(pressBtnBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = leftBtn;


//表表格处理
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height - 44.0) style:UITableViewStylePlain];
[tableView setDelegate:self];
[tableView setDataSource:self];
[tableView setShowsHorizontalScrollIndicator:NO];
[tableView setShowsVerticalScrollIndicator:NO];
[self.view addSubview:tableView];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

-(void) pressBtnBack{
[self.navigationController popViewControllerAnimated:YES];
}

-(void) getPhoneContacts{
ABAddressBookRef addressBook = nil;
if([[UIDevice currentDevice].systemVersion doubleValue] >= 6.0)
{
addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
addressBook = ABAddressBookCreate();
}

NSArray *temPeoples = (__bridge NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);
for(id temPerson in temPeoples)
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:2];
NSMutableArray *phoneArray = [[NSMutableArray alloc] initWithCapacity:3];

NSString *tmpFirstName = (__bridge NSString *) ABRecordCopyValue((__bridge ABRecordRef)(temPerson), kABPersonFirstNameProperty);
NSString *tmpLastName = (__bridge NSString *) ABRecordCopyValue((__bridge ABRecordRef)(temPerson), kABPersonLastNameProperty);

[dic setValue:[NSString stringWithFormat:@"%@ %@", tmpFirstName, tmpLastName] forKey:@"name"];
ABMultiValueRef phone = ABRecordCopyValue((__bridge ABRecordRef)(temPerson), kABPersonPhoneProperty);

for(int k = 0; k < ABMultiValueGetCount(phone); k++)
{
NSString *personPhone = (__bridge NSString *) ABMultiValueCopyValueAtIndex(phone, k);
[phoneArray addObject:personPhone];
}

[dic setValue:phoneArray forKey:@"phone"];
[resultArray addObject:dic];
}
}

-(NSInteger) tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [resultArray count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
}


NSDictionary *dic = [resultArray objectAtIndex:indexPath.row];
NSString *strName = [dic valueForKey:@"name"];

strName = [strName stringByReplacingOccurrencesOfString:@"(null)" withString:@""];

cell.textLabel.text = strName;
cell.detailTextLabel.text = @"邀请";
return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSelector:@selector(deleteSelectedCell) withObject:nil afterDelay:0.1];
NSDictionary *dic = [resultArray objectAtIndex:indexPath.row];
NSArray *numArray = [dic valueForKey:@"phone"];
UIActionSheet *sheet = [[UIActionSheet alloc] init];
[sheet setTitle:@"请选择号码"];
for(NSString *number in numArray)
{
[sheet addButtonWithTitle:number];
}

[sheet addButtonWithTitle:@"取消"];
[sheet setDelegate:self];
[sheet setCancelButtonIndex:[numArray count]];

[sheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
[sheet showInView:self.view];
}

-(void) deleteSelectedCell
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
}

-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *string = [actionSheet buttonTitleAtIndex:buttonIndex];
if([string isEqualToString:@"取消"])
{
return;
}

[self showMSMViewByNumber:string];
}

-(void) showMSMViewByNumber:(NSString *)string
{
if([MFMessageComposeViewController canSendText]){
[self displaySMSComposeSheet:string];
}else{
NSLog(@"Device not configured to send SMS.");
}
}

-(void) displaySMSComposeSheet:(NSString *)string
{
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
picker.messageComposeDelegate = self;
picker.body = @"老婆我爱你";
picker.recipients = [NSArray arrayWithObjects:string, nil];
[self presentModalViewController:picker animated:YES];
}

-(void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultSent:
NSLog(@"信息发送成功!");
break;
case MessageComposeResultFailed:
NSLog(@"信息发送失败!");
break;

default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}

@end

PhoneContactsViewController.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//
// PhoneContactsViewController.h
// PhoneContacts
//
// Created by david on 13-9-25.
// Copyright (c) 2013年 WalkerFree. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>

@interface PhoneContactsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate, MFMessageComposeViewControllerDelegate>

@property (strong, nonatomic) NSMutableArray *resultArray;
@property (strong, nonatomic) UITableView *tableView;

@end

别忘记引入文件:MessageUI和AddressBook