iOS获取手机通讯录(两种框架详解)
DouMota
8年前
<p>今天浏览简书,看了好多获取通讯录的,都是使用AddressBook.framework框架,没有使用Contacts.framework框架的,虽然这两种框架都可以获得手机里的通讯录信息,可是,官方API上说AddressBook.framework在iOS9被弃用了,弃用了,弃用了(重要是的事说三遍),又可是,不知道为什么目前使用AddressBook.framework在iOS9系统上依然可以获得联系人信息.........那么问题来了,如果哪天突然不能用了,而你又没有了解Contacts.framework框架那不就咖喱给给了...下面分别介绍两种框架(PS:其实用起来都差不多啦)<br> 前面截图较多,想直接使用代码的请拉到最下方。</p> <h2><br> 一、使用Contacts.framework框架</h2> <p>1、在项目中包含Contacts.framework和ContactsUI.framework<br> 2、当使用这个框架的时候, 系统会自动发送授权申请, 不需要手动授权,一言不合我就上截图</p> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/ceecb33b9a0a52b4eb0e757aec565a10.png"></p> <p><br> 不实现这选中多个联系人代理方法界面</p> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/e40e31c623cbb9628d7ae287aaddda80.png"><br> 实现代理方法的界面</p> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/671f2034effb748b968265974022b404.png"></p> <p>这四个代理方法可真他喵的事多,我慢慢给你说道说道<br> 一旦实现了 选中一个联系人或选中多个联系人方法 点击姓名或者完成之后 整个页面会直接dismiss,而不会跳转到联系人详情界面!!!!</p> <p>没有实现上两种代理方法,若实现多个属性方法页面也会dismiss(PS:作者也没研究懂这代理有卵用)</p> <p>总之若想让用户选择联系人某属性,则只实现选中一个属性代理即可,这时页面会跳转到联系人详情界面,其他的任意一个方法实现页面都会直接dismiss。</p> <p>下面是选中一个联系人代理方法输出的截图,具体是啥好好看看就懂了</p> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/6dd610d317cb55bfa7fb3a48842d8f96.png"></p> <p>获取了contact就可以依据属性获取其中的CNLabeledValue,进而获取CNPhoneNumber,获取手机号digital.</p> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/6cbab04d8940863a3657fabf30a1724c.png"></p> <p>这部分为直接用代码获取联系人信息</p> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/50729a552916a289aaa17f8eda70c1cd.png"></p> <h2>二、使用AddressBook.framework框架</h2> <p>1、使用UI界面<br> 包含框架 #import <AddressBook/AddressBook.h></p> <pre> <code> #import <AddressBookUI/AddressBookUI.h> 集成代理 ABPeoplePickerNavigationControllerDelegate</code></pre> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/fca73e583ca5dc67de8c87b7f0954a19.png"><br> 总体来说用起来和Contacts用起来没啥区别<br> 2、不带UI的</p> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/4cf70803dcd9e8dbb7cd51fa9f20488f.png"></p> <p><img alt="获取手机通讯录(两种框架详解)" src="https://simg.open-open.com/show/8be0a8c09f96b2d78bd87a0fd99ed313.png"></p> <p>最后部分是全部代码们,请各位童鞋自己对照着看吧<br> 一、address框架<br> 1、有UI</p> <pre> <code>- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { ABPeoplePickerNavigationController * peoplePickerNav = [ABPeoplePickerNavigationController new]; peoplePickerNav.peoplePickerDelegate = self; [self presentViewController:peoplePickerNav animated:YES completion:nil]; } //- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person { // // NSLog(@"选中了person,%@",person); //} - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{ NSLog(@"选中了属性,property:%d,identifier:%d",property,identifier); }</code></pre> <p>2、无UI</p> <pre> <code> //这个变量用于记录授权是否成功,即用户是否允许我们访问通讯录 int __block tip = 0; //声明一个通讯簿的引用 ABAddressBookRef addBook = nil; //创建通讯簿的引用 addBook = ABAddressBookCreateWithOptions(NULL, NULL); //创建一个出事信号量为0的信号 dispatch_semaphore_t sema = dispatch_semaphore_create(0); //申请访问权限 ABAddressBookRequestAccessWithCompletion(addBook, ^(bool greanted, CFErrorRef error) { //greanted为YES是表示用户允许,否则为不允许 if (!greanted) { tip = 1; } //发送一次信号 dispatch_semaphore_signal(sema); }); //等待信号触发 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); if (tip) { //做一个友好的提示 UIAlertView * alart = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请您设置允许APP访问您的通讯录\nSettings>General>Privacy" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alart show]; return; } //获取所有联系人的数组 CFArrayRef allLinkPeople = ABAddressBookCopyArrayOfAllPeople(addBook); //获取联系人总数 CFIndex number = ABAddressBookGetPersonCount(addBook); //进行遍历 for (int i = 0; i < number; i++) { //获取联系人对象的引用 ABRecordRef people = CFArrayGetValueAtIndex(allLinkPeople, i); //获取当前联系人名字 NSString * firstName = (__bridge NSString *)(ABRecordCopyValue(people, kABPersonFirstNameProperty)); //获取当前联系人姓氏 NSString * lastName=(__bridge NSString *)(ABRecordCopyValue(people, kABPersonLastNameProperty)); //获取当前联系人的名字拼音 NSString * firstNamePhoneic=(__bridge NSString*)(ABRecordCopyValue(people, kABPersonFirstNamePhoneticProperty)); //获取当前联系人的备注 NSString * notes = (__bridge NSString*)(ABRecordCopyValue(people, kABPersonNoteProperty)); //获取当前联系人的电话 数组 NSMutableArray * phoneArr = [[NSMutableArray alloc]init]; ABMultiValueRef phones= ABRecordCopyValue(people, kABPersonPhoneProperty); for (NSInteger j = 0; j < ABMultiValueGetCount(phones); j++) { [phoneArr addObject:(__bridge NSString *)(ABMultiValueCopyValueAtIndex(phones, j))]; } //获取当前联系人头像图片 NSData * userImage=(__bridge NSData*)(ABPersonCopyImageData(people));</code></pre> <p>二、Contacts框架<br> 1、有UI</p> <pre> <code> CNContactPickerViewController * contactPickerVc = [CNContactPickerViewController new]; contactPickerVc.delegate = self; [self presentViewController:contactPickerVc animated:YES completion:nil]; #pragma mark - 选中一个联系人 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact{ NSLog(@"contact:%@",contact); //phoneNumbers 包含手机号和家庭电话等 for (CNLabeledValue * labeledValue in contact.phoneNumbers) { CNPhoneNumber * phoneNumber = labeledValue.value; NSLog(@"phoneNum:%@", phoneNumber.stringValue); } } #pragma mark - 选中一个联系人属性 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty{ NSLog(@"contactProperty:%@",contactProperty); } #pragma mark - 选中一个联系人的多个属性 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty*> *)contactProperties{ NSLog(@"contactPropertiescontactProperties:%@",contactProperties); } #pragma mark - 选中多个联系人 - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts{ NSLog(@"contactscontacts:%@",contacts); }</code></pre> <p>2、无UI</p> <pre> <code> if (event.type == 0) { //判断授权状态 if ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts] == CNAuthorizationStatusNotDetermined) { CNContactStore *store = [[CNContactStore alloc] init]; [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { NSLog(@"授权成功"); // 2. 获取联系人仓库 CNContactStore * store = [[CNContactStore alloc] init]; // 3. 创建联系人信息的请求对象 NSArray * keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]; // 4. 根据请求Key, 创建请求对象 CNContactFetchRequest * request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys]; // 5. 发送请求 [store enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) { // 6.1 获取姓名 NSString * givenName = contact.givenName; NSString * familyName = contact.familyName; NSLog(@"%@--%@", givenName, familyName); // 6.2 获取电话 NSArray * phoneArray = contact.phoneNumbers; for (CNLabeledValue * labelValue in phoneArray) { CNPhoneNumber * number = labelValue.value; NSLog(@"%@--%@", number.stringValue, labelValue.label); } }]; } else { NSLog(@"授权失败"); } }]; }</code></pre> <p><br> 来源:http://www.jianshu.com/p/5c7548bf8c31</p>