使用场景
一些App通过手机号码来推荐好友,如 微博、支付宝
首先客户端会获取通讯录中的所有手机号然后将这些手机号提交到App服务器中,服务器会查找每个手机号对应的App账号如QQ号码返回到客户端,然后客户端根据服务器返回的账号列表来推荐好友。
获取联系人方式
方案一:AddressBookUI.framework框架
提供了联系人列表界面、联系人详情界面、添加联系人界面等
一般用于选择联系人
方案二:AddressBook.framework框架:
没有提供UI界面,需要自己搭建联系人界面
纯C语言的API, 仅仅是获得联系人数据
大部分数据类型是Core Foundation
从iOS6 开始,需要得到用户的授权才能访问通讯录
方案三:第三方框架:RHAddressBook
对 AddressBook.framework 进行封装
方案四:iOS9.0最新通讯录框架
ContactsUI.framework : 方案1的替代品,特点: 面向对象,使用简单,有界面
Contacts.framework: 方案2的替代品, 特点:面向对象,使用简单,五界面
方案一:AddressBookUI.framework
实现步骤:
创建选择联系人的控制器
设置代理:用来接收用户选择的联系人信息
弹出联系人控制器
实现代理方法
在对应的代理方法中获取联系人信息
AddressBook.frame
实现步骤:
请求授权
判断授权状态如果已授权则继续,如果未授权则提示用户
创建通讯录对象
从通讯录中获取所有的联系人
遍历所有的联系人
释放不再使用的对象
AddreesBook.framework具体实现:
1. AppDelegate 应用启动时请求授权
#import"AppDelegate.h"#import@interfaceAppDelegate()@end@implementationAppDelegate- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {// Override point for customization after application launch.[selfrequestAuthorizationAddressBook];returnYES;}- (void)requestAuthorizationAddressBook {// 判断是否授权ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();if(authorizationStatus == kABAuthorizationStatusNotDetermined) {// 请求授权ABAddressBookRef addressBookRef = ABAddressBookCreate(); ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(boolgranted, CFErrorRef error) {if(granted) {// 授权成功}else{// 授权失败NSLog(@"授权失败!"); } }); }}@end
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
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
2. iOS10 需要在Info.plist配置NSContactsUsageDescription
NSContactsUsageDescription请求访问通讯录
1
2
1
2
3. ViewController
#import"ViewController.h"#import@interfaceViewController()@end@implementationViewController- (void)viewDidLoad { [superviewDidLoad];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {// 1. 判读授权ABAuthorizationStatus authorizationStatus = ABAddressBookGetAuthorizationStatus();if(authorizationStatus != kABAuthorizationStatusAuthorized) {NSLog(@"没有授权");return; }// 2. 获取所有联系人ABAddressBookRef addressBookRef = ABAddressBookCreate(); CFArrayRef arrayRef = ABAddressBookCopyArrayOfAllPeople(addressBookRef);longcount = CFArrayGetCount(arrayRef);for(inti =0; i < count; i++) {//获取联系人对象的引用ABRecordRef people = CFArrayGetValueAtIndex(arrayRef, i);//获取当前联系人名字NSString*firstName=(__bridgeNSString*)(ABRecordCopyValue(people, kABPersonFirstNameProperty));//获取当前联系人姓氏NSString*lastName=(__bridgeNSString*)(ABRecordCopyValue(people, kABPersonLastNameProperty));NSLog(@"--------------------------------------------------");NSLog(@"firstName=%@, lastName=%@", firstName, lastName);//获取当前联系人的电话 数组NSMutaleArray *phoneArray = [[NSMutableArrayalloc]init]; ABMultiValueRef phones = ABRecordCopyValue(people, kABPersonPhoneProperty);for(NSIntegerj=0; j
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
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
4. 运行结果
第三方框架:RHAddressBook
https://github.com/heardrwt/RHAddressBook
该框架使用的MRC来管理内存的,如果直接将源代码拖入进去需要为每个文件设置编译标记:-fno-objc-arc, 设置完还会报错,该项目使用的一些方法过于古老,很多都不支持了,所以这种方式不采用; 可以将该项目打成静态库的方式;也可以直接将项目拖入到自己的工程中作为一个依赖
直接将RHAddressBook.xcodeproj拖入到工程中
添加Target Dependencies和Link Binary With Libraries
Build Settings—> Other Linker Flags : -ObjC
用于解决系统分类找不到方法的错误
iOS10 需要在Info.plist配置NSContactsUsageDescription
NSContactsUsageDescription请求访问通讯录
1
2
1
2
App启动时请求授权访问通讯录
#import"AppDelegate.h"#import@interfaceAppDelegate()@end@implementationAppDelegate- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {// Override point for customization after application launch.[selfrequestAuthorizationForAddressBook];returnYES;}- (void)requestAuthorizationForAddressBook { RHAddressBook *ab = [[RHAddressBook alloc] init];if([RHAddressBook authorizationStatus] == RHAuthorizationStatusNotDetermined){ [ab requestAuthorizationWithCompletion:^(boolgranted,NSError*error) {if(granted) { }else{NSLog(@"请求授权拒绝"); } }]; }}@end
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
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
获取所有联系人的信息:姓名、手机号等
#import"ViewController.h"#import#import@interfaceViewController()@end@implementationViewController- (void)viewDidLoad { [superviewDidLoad];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { RHAddressBook *addressBook = [[RHAddressBook alloc] init];if([RHAddressBook authorizationStatus] != RHAuthorizationStatusAuthorized){NSLog(@"没有授权");return; }NSArray*peopleArray= addressBook.people;for(inti =0; i < peopleArray.count; i++) { RHPerson *people = (RHPerson *)peopleArray[i];NSLog(@"%@", people.name); RHMultiStringValue *phoneNumbers = people.phoneNumbers;for(inti =0; i < phoneNumbers.count; i++) {NSString* label= [phoneNumbers labelAtIndex:i];NSString* value= [phoneNumbers valueAtIndex:i];NSLog(@"label=%@, value=%@", label, value); }NSLog(@"----------------------------------------------"); }}@end
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
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
运行结果:
ContactsUI.framework
#import"ViewController.h"#import@interfaceViewController() @end@implementationViewController- (void)viewDidLoad { [superviewDidLoad]; CNContactPickerViewController *contactPickerViewController = [[CNContactPickerViewController alloc] init]; contactPickerViewController.delegate=self; [selfpresentViewController:contactPickerViewController animated:YEScompletion:nil];}// 如果实现该方法当选中联系人时就不会再出现联系人详情界面, 如果需要看到联系人详情界面只能不实现这个方法,- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {NSLog(@"选中某一个联系人时调用---------------------------------"); [selfprintContactInfo:contact];}// 同时选中多个联系人- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray *)contacts {for(CNContact *contact in contacts) {NSLog(@"================================================"); [selfprintContactInfo:contact]; }}- (void)printContactInfo:(CNContact *)contact {NSString*givenName = contact.givenName;NSString*familyName = contact.familyName;NSLog(@"givenName=%@, familyName=%@", givenName, familyName);NSArray* phoneNumbers = contact.phoneNumbers;for(CNLabeledValue*phone in phoneNumbers) {NSString*label = phone.label; CNPhoneNumber *phonNumber = (CNPhoneNumber *)phone.value;NSLog(@"label=%@, value=%@", label, phonNumber.stringValue); }}// 注意:如果实现该方法,上面那个方法就不能实现了,这两个方法只能实现一个//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {// NSLog(@"选中某个联系人的某个属性时调用");//}@end
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
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
选择单个联系人时运行效果:
选择多个联系人的界面:
Contact.framework
iOS10 需要在Info.plist配置NSContactsUsageDescription
NSContactsUsageDescription请求访问通讯录
1
2
1
2
应用启动时请求授权:
#import"AppDelegate.h"#import@interfaceAppDelegate()@end@implementationAppDelegate- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {// Override point for customization after application launch.[selfrequestAuthorizationForAddressBook];returnYES;}- (void)requestAuthorizationForAddressBook { CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];if(authorizationStatus == CNAuthorizationStatusNotDetermined) { CNContactStore *contactStore = [[CNContactStore alloc] init]; [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOLgranted,NSError* _Nullable error) {if(granted) { }else{NSLog(@"授权失败, error=%@", error); } }]; }}@end
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
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
获取通讯录信息
#import"ViewController.h"#import@interfaceViewController()@end@implementationViewController- (void)viewDidLoad { [superviewDidLoad];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];if(authorizationStatus == CNAuthorizationStatusAuthorized) {NSLog(@"没有授权..."); }// 获取指定的字段,并不是要获取所有字段,需要指定具体的字段NSArray*keysToFetch = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]; CNContactFetchRequest *fetchRequest = [[CNContactFetchRequest alloc] initWithKeysToFetch:keysToFetch]; CNContactStore *contactStore = [[CNContactStore alloc] init]; [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nilusingBlock:^(CNContact * _Nonnull contact,BOOL* _Nonnull stop) {NSLog(@"-------------------------------------------------------");NSString*givenName = contact.givenName;NSString*familyName = contact.familyName;NSLog(@"givenName=%@, familyName=%@", givenName, familyName);NSArray*phoneNumbers = contact.phoneNumbers;for(CNLabeledValue *labelValue in phoneNumbers) {NSString*label = labelValue.label; CNPhoneNumber *phoneNumber = labelValue.value;NSLog(@"label=%@, phone=%@", label, phoneNumber.stringValue); }// *stop = YES; // 停止循环,相当于break;}];}@end
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
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
运行效果:
顶
0
踩
0
上