ios 獲取手機通訊錄數據

ios 獲取手機數據是方式有兩種框架可以實現:ios9 之前的AddressBook.framework 和 ios9之后的Contacts.framework。

AddressBook.framework 使用(送授權申請, 需要手動授權。需要引用 <AddressBook/AddressBook.h>)

 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);
                        }
                    }];
                }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容