通讯录功能的实现
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 123 for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++) 124 125 { 126 127 NSString* tmpPhoneIndex = (NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j); 128 129 NSLog(@"tmpPhoneIndex%d:%@", j, tmpPhoneIndex); 130 131 [tmpPhoneIndex release]; 132 133 } 134 135 CFRelease(tmpPhones); 136 137 } 138 139 //释放内存 140 141 [tmpPeoples release]; 142 143 CFRelease(tmpAddressBook); 144 145 }
解释下代码:由于ios6.0系统的升级,对客户隐私有了一个较大的提示。
以前
1 tmpAddressBook =ABAddressBookCreate();这样就可以读取了,但是现在要做一个判断了。可以具体看看代码。
- 把通讯录取出来放在一个array里面。这样你就可以对数组进行你的要求了。
备注:
苹果中文语言貌似有点问题。ios6.0 会提示你的app访问日历。。。。
然后网上查了下资料。可以参照微信访问通讯录的形式,给客户一个提示
他是在plist中 添加一项在XCode里显示的是Privacy - Contacts Usage Description。后面的string就是你要想提示的文字内容
“Privacy
- Location Usage Description”。这个是定位位置提示。其他类似。
备注二:
分享一个测试经验,也是从网上看到的。
一把苹果的app访问位置或者通讯录,第一次都会有一个提示让你选择。
但是第二次及以后,即时删掉,也不会再出现了。
如果想再次出现,可以 设置-通用-还原-还原位置与隐私
这样你再次点击你的app就会再次出现了。
时间: 2024-11-02 13:45:34