iOS通訊錄介紹和使用

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

一.iOS9之前:AddressBookUI的使用:

1.導入頭文件,遵守協議

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

@interface ViewController () <ABPeoplePickerNavigationControllerDelegate>
@end

2.創建選擇聯系人的控制器

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

3.實現代理方法

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

// 當用戶選中某一個聯系人的某一個屬性時會執行該方法,并且選中屬性后會退出控制器
- (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.獲取授權狀態
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    
    // 2.如果是已經授權,才能獲取聯系人
    if (status != kABAuthorizationStatusAuthorized) return;
    
    // 3.創建通信錄對象
    ABAddressBookRef addressBook = ABAddressBookCreate();
    
    // 4.獲取所有的聯系人
    CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex peopleCount = CFArrayGetCount(peopleArray);
    
    // 5.遍歷所有的聯系人
    for (int i = 0; i < peopleCount; i++) {
        // 5.1.獲取某一個聯系人
        ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i);
        // 5.2.獲取聯系人的姓名
        NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
        NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSLog(@"%@ %@", lastName, firstName);
        
        // 5.3.獲取電話號碼
        // 5.3.1.獲取所有的電話號碼
        ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        CFIndex phoneCount = ABMultiValueGetCount(phones);
        
        // 5.3.2.遍歷拿到每一個電話號碼
        for (int i = 0; i < phoneCount; i++) {
            // 1.獲取電話對應的key
            NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
            
            // 2.獲取電話號碼
            NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
            
            NSLog(@"%@ %@", phoneLabel, phoneValue);
        }
        
        CFRelease(phones);
    }
    
    CFRelease(addressBook);
    CFRelease(peopleArray);
}

三.iOS9之后:ContactsUI的使用

1.創建選擇聯系人的控制器

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

2.實現代理方法

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

// 當選中某一個聯系人的某一個屬性時會執行該方法
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty
{
}

// 點擊了取消按鈕會執行該方法
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker
{  
}

四.iOS9之后:Contacts的使用

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1.獲取授權狀態
    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
    
    // 2.判斷授權狀態,如果不是已經授權,則直接返回
    if (status != CNAuthorizationStatusAuthorized) return;
    
    // 3.創建通信錄對象
    CNContactStore *contactStore = [[CNContactStore alloc] init];
    
    // 4.創建獲取通信錄的請求對象
    // 4.1.拿到所有打算獲取的屬性對應的key
    NSArray *keys = @[CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey];
    
    // 4.2.創建CNContactFetchRequest對象
    CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
    
    // 5.遍歷所有的聯系人
    [contactStore enumerateContactsWithFetchRequest:request error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
        // 1.獲取聯系人的姓名
        NSString *lastname = contact.familyName;
        NSString *firstname = contact.givenName;
        NSLog(@"%@ %@", lastname, firstname);
        
        // 2.獲取聯系人的電話號碼
        NSArray *phoneNums = contact.phoneNumbers;
        for (CNLabeledValue *labeledValue in phoneNums) {
            // 2.1.獲取電話號碼的KEY
            NSString *phoneLabel = labeledValue.label;
            
            // 2.2.獲取電話號碼
            CNPhoneNumber *phoneNumer = labeledValue.value;
            NSString *phoneValue = phoneNumer.stringValue;
            
            NSLog(@"%@ %@", phoneLabel, phoneValue);
        }
    }];
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容