iOS开发——高级技术&通讯录功能的实现
通讯录功能的实现
iOS 提供了对通讯录操作的组建,其中一个是直接操作通讯录,另一个是调用通讯录的 UI 组建。实现方法如下:
添加AddressBook.framework到工程中。
代码实现:
1 -(IBAction)onClickbutton:(id)sender
2 {
3 NSMutableArray* personArray = [[[NSMutableArray alloc] init] autorelease];
4 ABAddressBookRef addressBook = ABAddressBookCreate();
5 NSString *firstName, *lastName, *fullName;
6 personArray = (NSMutableArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
7 if ([sender tag]==0) {
8
9 for (id *person in personArray)
10 {
11 firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
12 firstName = [firstName stringByAppendingFormat:@" "];
13 lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
14 fullName = [firstName stringByAppendingFormat:@"%@",lastName];
15 NSLog(@"===%@",fullName);
16 ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonPhoneProperty);
17 for(int i = 0 ;i < ABMultiValueGetCount(phones); i++)
18 {
19 NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
20 NSLog(@"===%@",phone);
21 }
22 ABMultiValueRef mails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
23 for(int i = 0 ;i < ABMultiValueGetCount(mails); i++)
24 {
25 NSString *mail = (NSString *)ABMultiValueCopyValueAtIndex(mails, i);
26 NSLog(@"==%@",mail);
27 }
28 }
29 }else {
30 //删除信息
31 //返回所有联系人到一个数组中
32 CFArrayRef personArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
33 CFIndex personCount = ABAddressBookGetPersonCount(addressBook);
34 for (int i =0;i<personCount;i++){
35 ABRecordRef ref = CFArrayGetValueAtIndex(personArray, i);
36 CFStringRef firstName1 = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
37 CFStringRef lastName1 = ABRecordCopyValue(ref, kABPersonLastNameProperty);
38 NSString *contactFirstLast = [NSString stringWithFormat: @"%@%@", (NSString *)firstName1,(NSString *)lastName1];
39 if ([contactFirstLast isEqualToString:@"徐梦"]) {
40 //删除联系人
41 ABAddressBookRemoveRecord(addressBook, ref, nil);
42 }
43 }
44 //保存电话本
45 ABAddressBookSave(addressBook, nil);
46 //释放内存
47 //CFRelease(personRef);
48 // CFRelease(addressbookRef);
49 }
50 }
完整实现(一个项目要求app能读取手机的通讯录)
其实我是反对这类的需求,你说你读我的隐私,我肯定不愿意的。幸好ios6.0 以后给了个权限控制。当打开app的时候你可以选择拒绝。
实现方法:
1 //读取所有联系人
2
3 -(void)ReadAllPeoples
4
5 {
6
7 //取得本地通信录名柄
8
9 ABAddressBookRef tmpAddressBook = nil;
10
11 if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0) {
12 tmpAddressBook=ABAddressBookCreateWithOptions(NULL, NULL);
13 dispatch_semaphore_t sema=dispatch_semaphore_create(0);
14 ABAddressBookRequestAccessWithCompletion(tmpAddressBook, ^(bool greanted, CFErrorRef error){
15 dispatch_semaphore_signal(sema);
16 });
17
18 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
19 dispatch_release(sema);
20 }
21 else
22 {
23 tmpAddressBook =ABAddressBookCreate();
24 }
25 //取得本地所有联系人记录
26
27
28 if (tmpAddressBook==nil) {
29 return ;
30 };
31 NSArray* tmpPeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);
32
33 for(id tmpPerson in tmpPeoples)
34
35 {
36
37 //获取的联系人单一属性:First name
38
39 NSString* tmpFirstName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonFirstNameProperty);
40
41 NSLog(@"First name:%@", tmpFirstName);
42
43 [tmpFirstName release];
44
45 //获取的联系人单一属性:Last name
46
47 NSString* tmpLastName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonLastNameProperty);
48
49 NSLog(@"Last name:%@", tmpLastName);
50
51 [tmpLastName release];
52
53 //获取的联系人单一属性:Nickname
54
55 NSString* tmpNickname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNicknameProperty);
56
57 NSLog(@"Nickname:%@", tmpNickname);
58
59 [tmpNickname release];
60
61 //获取的联系人单一属性:Company name
62
63 NSString* tmpCompanyname = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonOrganizationProperty);
64
65 NSLog(@"Company name:%@", tmpCompanyname);
66
67 [tmpCompanyname release];
68
69 //获取的联系人单一属性:Job Title
70
71 NSString* tmpJobTitle= (NSString*)ABRecordCopyValue(tmpPerson, kABPersonJobTitleProperty);
72
73 NSLog(@"Job Title:%@", tmpJobTitle);
74
75 [tmpJobTitle release];
76
77 //获取的联系人单一属性:Department name
78
79 NSString* tmpDepartmentName = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonDepartmentProperty);
80
81 NSLog(@"Department name:%@", tmpDepartmentName);
82
83 [tmpDepartmentName release];
84
85 //获取的联系人单一属性:Email(s)
86
87 ABMultiValueRef tmpEmails = ABRecordCopyValue(tmpPerson, kABPersonEmailProperty);
88
89 for(NSInteger j = 0; ABMultiValueGetCount(tmpEmails); j++)
90
91 {
92
93 NSString* tmpEmailIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpEmails, j);
94
95 NSLog(@"Emails%d:%@", j, tmpEmailIndex);
96
97 [tmpEmailIndex release];
98
99 }
100
101 CFRelease(tmpEmails);
102
103 //获取的联系人单一属性:Birthday
104
105 NSDate* tmpBirthday = (NSDate*)ABRecordCopyValue(tmpPerson, kABPersonBirthdayProperty);
106
107 NSLog(@"Birthday:%@", tmpBirthday);
108
109 [tmpBirthday release];
110
111 //获取的联系人单一属性:Note
112
113 NSString* tmpNote = (NSString*)ABRecordCopyValue(tmpPerson, kABPersonNoteProperty);
114
115 NSLog(@"Note:%@", tmpNote);
116
117 [tmpNote release];
118
119 //获取的联系人单一属性:Generic phone number
120
121 ABMultiValueRef tmpPhones = ABRecordCopyValue(tmpPerson, kABPersonPhoneProperty);
122