iOS通訊錄介紹和使用

通訊錄的介紹.png
通訊錄的使用.png

一.iOS9之前:AddressBookUI的使用:

1.導(dǎo)入頭文件,遵守協(xié)議

#import "ViewController.h"
#import <AddressBookUI/AddressBookUI.h>

@interface ViewController () <ABPeoplePickerNavigationControllerDelegate>
@end

2.創(chuàng)建選擇聯(lián)系人的控制器

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1.創(chuàng)建選擇聯(lián)系人的控制器
    ABPeoplePickerNavigationController *ppnc = [[ABPeoplePickerNavigationController alloc] init];
    
    // 2.設(shè)置代理
    ppnc.peoplePickerDelegate = self;
    
    // 3.彈出控制器
    [self presentViewController:ppnc animated:YES completion:nil];
}

3.實(shí)現(xiàn)代理方法

#pragma mark - <ABPeoplePickerNavigationControllerDelegate>
// 當(dāng)用戶選中某一個(gè)聯(lián)系人時(shí)會(huì)執(zhí)行該方法,并且選中聯(lián)系人后會(huì)直接退出控制器
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
    // 1.獲取選中聯(lián)系人的姓名
    CFStringRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    CFStringRef firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    
    // (__bridge NSString *) : 將對(duì)象交給Foundation框架的引用來使用,但是內(nèi)存不交給它來管理
    // (__bridge_transfer NSString *) : 將對(duì)象所有權(quán)直接交給Foundation框架的應(yīng)用,并且內(nèi)存也交給它來管理
    NSString *lastname = (__bridge_transfer NSString *)(lastName);
    NSString *firstname = (__bridge_transfer NSString *)(firstName);
    
    NSLog(@"%@ %@", lastname, firstname);
    
    // 2.獲取選中聯(lián)系人的電話號(hào)碼
    // 2.1.獲取所有的電話號(hào)碼
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    CFIndex phoneCount = ABMultiValueGetCount(phones);
    
    // 2.2.遍歷拿到每一個(gè)電話號(hào)碼
    for (int i = 0; i < phoneCount; i++) {
        // 2.2.1.獲取電話對(duì)應(yīng)的key
        NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
        
        // 2.2.2.獲取電話號(hào)碼
        NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
        
        NSLog(@"%@ %@", phoneLabel, phoneValue);
    }
    
   // 注意:管理內(nèi)存 
    CFRelease(phones);
}

// 當(dāng)用戶選中某一個(gè)聯(lián)系人的某一個(gè)屬性時(shí)會(huì)執(zhí)行該方法,并且選中屬性后會(huì)退出控制器
- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    NSLog(@"%s", __func__);
}

二.iOS9之前:AddressBook的使用

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1.獲取授權(quán)狀態(tài)
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    
    // 2.如果是已經(jīng)授權(quán),才能獲取聯(lián)系人
    if (status != kABAuthorizationStatusAuthorized) return;
    
    // 3.創(chuàng)建通信錄對(duì)象
    ABAddressBookRef addressBook = ABAddressBookCreate();
    
    // 4.獲取所有的聯(lián)系人
    CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex peopleCount = CFArrayGetCount(peopleArray);
    
    // 5.遍歷所有的聯(lián)系人
    for (int i = 0; i < peopleCount; i++) {
        // 5.1.獲取某一個(gè)聯(lián)系人
        ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i);
        // 5.2.獲取聯(lián)系人的姓名
        NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
        NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSLog(@"%@ %@", lastName, firstName);
        
        // 5.3.獲取電話號(hào)碼
        // 5.3.1.獲取所有的電話號(hào)碼
        ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        CFIndex phoneCount = ABMultiValueGetCount(phones);
        
        // 5.3.2.遍歷拿到每一個(gè)電話號(hào)碼
        for (int i = 0; i < phoneCount; i++) {
            // 1.獲取電話對(duì)應(yīng)的key
            NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
            
            // 2.獲取電話號(hào)碼
            NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
            
            NSLog(@"%@ %@", phoneLabel, phoneValue);
        }
        
        CFRelease(phones);
    }
    
    CFRelease(addressBook);
    CFRelease(peopleArray);
}

三.iOS9之后:ContactsUI的使用

1.創(chuàng)建選擇聯(lián)系人的控制器

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1.創(chuàng)建選擇聯(lián)系人的控制器
    CNContactPickerViewController *contactVc = [[CNContactPickerViewController alloc] init];
    
    // 2.設(shè)置代理
    contactVc.delegate = self;
    
    // 3.彈出控制器
    [self presentViewController:contactVc animated:YES completion:nil];
}

2.實(shí)現(xiàn)代理方法

#pragma mark - <CNContactPickerDelegate>
// 當(dāng)選中某一個(gè)聯(lián)系人時(shí)會(huì)執(zhí)行該方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact
{
    // 1.獲取聯(lián)系人的姓名
    NSString *lastname = contact.familyName;
    NSString *firstname = contact.givenName;
    NSLog(@"%@ %@", lastname, firstname);
    
    // 2.獲取聯(lián)系人的電話號(hào)碼
    NSArray *phoneNums = contact.phoneNumbers;
    for (CNLabeledValue *labeledValue in phoneNums) {
        // 2.1.獲取電話號(hào)碼的KEY
        NSString *phoneLabel = labeledValue.label;
        
        // 2.2.獲取電話號(hào)碼
        CNPhoneNumber *phoneNumer = labeledValue.value;
        NSString *phoneValue = phoneNumer.stringValue;
        
        NSLog(@"%@ %@", phoneLabel, phoneValue);
    }
}

// 當(dāng)選中某一個(gè)聯(lián)系人的某一個(gè)屬性時(shí)會(huì)執(zhí)行該方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
}

// 點(diǎn)擊了取消按鈕會(huì)執(zhí)行該方法
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{  
}

四.iOS9之后:Contacts的使用

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1.獲取授權(quán)狀態(tài)
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    
    // 2.判斷授權(quán)狀態(tài),如果不是已經(jīng)授權(quán),則直接返回
    if (status != CNAuthorizationStatusAuthorized) return;
    
    // 3.創(chuàng)建通信錄對(duì)象
    CNContactStore *contactStore = [[CNContactStore alloc] init];
    
    // 4.創(chuàng)建獲取通信錄的請(qǐng)求對(duì)象
    // 4.1.拿到所有打算獲取的屬性對(duì)應(yīng)的key
    NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    
    // 4.2.創(chuàng)建CNContactFetchRequest對(duì)象
    CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
    
    // 5.遍歷所有的聯(lián)系人
    [contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
        // 1.獲取聯(lián)系人的姓名
        NSString *lastname = contact.familyName;
        NSString *firstname = contact.givenName;
        NSLog(@"%@ %@", lastname, firstname);
        
        // 2.獲取聯(lián)系人的電話號(hào)碼
        NSArray *phoneNums = contact.phoneNumbers;
        for (CNLabeledValue *labeledValue in phoneNums) {
            // 2.1.獲取電話號(hào)碼的KEY
            NSString *phoneLabel = labeledValue.label;
            
            // 2.2.獲取電話號(hào)碼
            CNPhoneNumber *phoneNumer = labeledValue.value;
            NSString *phoneValue = phoneNumer.stringValue;
            
            NSLog(@"%@ %@", phoneLabel, phoneValue);
        }
    }];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容