ios 获取手机通讯录数据

ios 获取手机数据是方式有两种框架可以实现:ios9 之前的AddressBook.framework 和 ios9之后的Contacts.framework。

AddressBook.framework 使用(送授权申请, 需要手动授权。需要引用

 NSMutableArray *array = [NSMutableArray array];
    if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        //2. 创建通讯录
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
        //3. 获取所有联系人
        CFArrayRef peosons = ABAddressBookCopyArrayOfAllPeople(addressBook);
        //4. 遍历所有联系人来获取数据(姓名和电话)
        CFIndex count = CFArrayGetCount(peosons);
        for (CFIndex i = 0 ; i < count; i++) {
            //5. 获取单个联系人
            ABRecordRef person = CFArrayGetValueAtIndex(peosons, i);
            //6. 获取姓名
            NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
            NSString *firstName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
 ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
            //7.1 获取电话的count数
            CFIndex phoneCount = ABMultiValueGetCount(phones);
            //7.2 遍历所有电话号码
            for (CFIndex i = 0; i < phoneCount; i++) {
                NSString *label = CFBridgingRelease(ABMultiValueCopyLabelAtIndex(phones, i));
                NSString *value = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
                
               // 删除手机号码里除了了数字之处的数据(如果-)
                NSCharacterSet *setToRemove = [[ NSCharacterSet characterSetWithCharactersInString:@"0123456789"]
                                               invertedSet ];
                NSString *strPhone = [[value componentsSeparatedByCharactersInSet:setToRemove] componentsJoinedByString:@""];
                
                model.phoneNumber = strPhone;
                // 打印标签和电话号
                NSLog(@"label: %@, value: %@",label, value);
            }
            NSLog(@"\\n\\n");
            //8.1 释放 CF 对象
            CFRelease(phones);
            [array addObject:model];
        }
        
        //8.1 释放 CF 对象
        CFRelease(peosons);
        CFRelease(addressBook);

Contacts.framework 使用:(系统会自动发送授权申请, 不需要手动授权。 需要引用Contacts.framework和ContactsUI.framework)

 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);
                        }
                    }];
                }

你可能感兴趣的:(ios 获取手机通讯录数据)