- 实现步骤-及代码
// 1. 请求授权 // 1.获取授权的状态 ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); // 2.判断授权状态,如果是未决定状态,才需要请求 if (status == kABAuthorizationStatusNotDetermined) { // 2.1.创建通信录对象 ABAddressBookRef addressBook = ABAddressBookCreate(); // 2.2.请求授权 ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { if (granted) { NSLog(@"授权成功"); } else { NSLog(@"授权失败"); } }); } // 2. 判断授权状态, 如果已授权, 则继续; 未授权, 则提示用户, 并返回; // 1.获取授权的状态 ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus(); // 2.如果用户已经授权 if (status != kABAuthorizationStatusAuthorized) return; // 3. 创建通讯录对象 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL); // 4. 从通信录对象中, 获取所有的联系人 CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook); // 5. 遍历所有的联系人(每一个联系人都是一条记录) CFIndex peopleCount = CFArrayGetCount(peopleArray); for (CFIndex i = 0; i < peopleCount; i++) { // 6.获取到联系人 ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i); // 7.获取姓名 NSString *lastname = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); NSLog(@"%@ %@", lastname, firstName); // 8. 获取电话号码 ABMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonPhoneProperty); CFIndex count = ABMultiValueGetCount(multiValue); for (int i = 0; i < count; i ++) { NSString *label = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(multiValue, i); NSString *phone = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(multiValue, i); NSLog(@"%@---%@", label, phone); } CFRelease(multiValue); } 9. 释放不再使用的对象 CFRelease(peopleArray); CFRelease(addressBook);
获取通讯录-AddressBook(OC)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言iOS通讯录框架有AddressBook,AddressBookUI,Contacts,ContactsUI....