iOS通訊錄 AddressBook框架

  • 前言
    iOS通訊錄框架有AddressBook,AddressBookUI,Contacts,ContactsUI.
    iOS9 AddressBook,AddressBookUI不推薦使用了,用Contacts,ContactsUI代替。
    如果項(xiàng)目支持到iOS8或以下,還是使用AddressBook,AddressBookUI。本篇只涉及AddressBook框架的4個(gè)類。
    可以看到AddressBook包含內(nèi)容如下圖:

    屏幕快照 2016-04-07 下午9.54.27.png

  • 授權(quán)
    首先導(dǎo)入頭文件
    #import <AddressBook/AddressBook.h>
    請求授權(quán)的代碼

//如果用戶沒決定過,請求授權(quán)
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        //創(chuàng)建通訊錄
        ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
        //請求授權(quán)
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {//請求授權(quán)頁面用戶同意授權(quán)
                //讀取通訊錄人員數(shù)量,此處不可使用上面請求授權(quán)的通訊錄對象,會崩潰
                ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
                //獲取聯(lián)系人數(shù)量
                CFIndex personCount = ABAddressBookGetPersonCount(addressBook);
                dispatch_async(dispatch_get_main_queue(), ^{
                    //xxx
                });
                CFRelease(addressBook);
            }
         CFRelease(addressBookRef);
//如果是已授權(quán)狀態(tài)
    } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        //創(chuàng)建通訊錄
        //讀取通訊錄人員數(shù)量
        ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
        CFIndex personCount = ABAddressBookGetPersonCount(addressBookRef);
        //xxx
        CFRelease(addressBookRef);
    } else {
        [self showAlertWithTitle:@"提醒" message:@"請?jiān)谠O(shè)置中打開通訊錄授權(quán)"];
    }

授權(quán)狀態(tài)一共有四個(gè)值,可以自己點(diǎn)擊去頭文件看一下。為什么release,CoreFoundation框架內(nèi)存需要手動(dòng)管理,遇到create,copy,new,retain都需要release。

  • 讀取聯(lián)系人信息
    代碼:
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    //拿到所有聯(lián)系人
    CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
    //數(shù)組數(shù)量
    CFIndex peopleCount = CFArrayGetCount(peopleArray);
    for (int i = 0; i < peopleCount; i++) {
        //拿到一個(gè)人
        ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i);
        //拿到姓名
        //姓
        NSString *lastNameValue = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
        //名
        NSString *firstNameValue = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);

        //拿到多值電話
        ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        //多值數(shù)量
        CFIndex phoneCount = ABMultiValueGetCount(phones);
        for (int j = 0; j < phoneCount ; j++) {
            //電話標(biāo)簽本地化(例如是住宅,工作等)
            NSString *phoneLabel = (__bridge_transfer NSString *)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phones, j));
            //拿到標(biāo)簽下對應(yīng)的電話號碼
            NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, j);
        }
        CFRelease(phones);

        //郵箱多值
        ABMultiValueRef emails = ABRecordCopyValue(person, kABPersonEmailProperty);
        CFIndex emailCount = ABMultiValueGetCount(emails);
        for (int k = 0; k < emailCount; k++) {
            NSString *emailLabel = (__bridge_transfer NSString *)ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emails, k));
            NSString *emailValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, k);
        }
        CFRelease(emails);
    }
    CFRelease(addressBook);
    CFRelease(peopleArray);

關(guān)于__bridge_transfer,是橋接的關(guān)鍵字之一,還有__bridge_retained和__bridge,可以自行搜索橋接如有不明白,關(guān)鍵的是轉(zhuǎn)換對象所有權(quán)的問題。

  • 新增聯(lián)系人
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    for (IABPerson *iPerson in array) {
        //創(chuàng)建一個(gè)聯(lián)系人
        ABRecordRef person = ABPersonCreate();
        //新增姓名
        NSString *Name = iPerson.Name;
        //轉(zhuǎn)換為CFString
        CFStringRef lastName = (__bridge_retained CFStringRef)Name;
        //設(shè)置屬性
        ABRecordSetValue(person, kABPersonLastNameProperty, lastName, NULL);
        CFRelease(lastName);
        //新增電話
        ABMultiValueRef phones = ABMultiValueCreateMutable(kABMultiStringPropertyType);
            //手機(jī)標(biāo)簽設(shè)置值
            CFStringRef mobile = (__bridge_retained CFStringRef)iPerson.MobilePhone;
            ABMultiValueAddValueAndLabel(phones, mobile, kABPersonPhoneMobileLabel, NULL);
            CFRelease(mobile);
            //住宅標(biāo)簽設(shè)置值
            CFStringRef homeTel = (__bridge_retained CFStringRef)iPerson.HomeTel;
            ABMultiValueAddValueAndLabel(phones, homeTel, kABHomeLabel, NULL);
            CFRelease(homeTel);
            //工作標(biāo)簽設(shè)置值
            CFStringRef workTel = (__bridge_retained CFStringRef)iPerson.WorkTel;
            ABMultiValueAddValueAndLabel(phones, workTel, kABWorkLabel, NULL);
            CFRelease(workTel);
            //其他標(biāo)簽設(shè)置值
            CFStringRef otherTel = (__bridge_retained CFStringRef)iPerson.OtherTel;
            ABMultiValueAddValueAndLabel(phones, otherTel, kABOtherLabel, NULL);
            CFRelease(otherTel);
        //為聯(lián)系人的電話多值 設(shè)置值
        ABRecordSetValue(person, kABPersonPhoneProperty, phones, NULL);

        //新增郵箱
        ABMultiValueRef emails = ABMultiValueCreateMutable(kABPersonEmailProperty);
            //住宅郵箱設(shè)置值
            CFStringRef email = (__bridge_retained CFStringRef)iPerson.Email;
            ABMultiValueAddValueAndLabel(emails, email, kABHomeLabel, NULL);
            CFRelease(email);
        //為聯(lián)系人添加郵箱多值
        ABRecordSetValue(person, kABPersonEmailProperty, emails, NULL);
        //給通訊錄添加聯(lián)系人
        ABAddressBookAddRecord(addressBook, person, NULL);
        CFRelease(person);
        CFRelease(phones);
        CFRelease(emails);
    }
    //保存通訊錄,一定要保存
    ABAddressBookSave(addressBook, NULL);
    CFRelease(addressBook);

這里有一點(diǎn)就是,電話下面的標(biāo)簽,如果你在電話屬性下面找

屏幕快照 2016-04-07 下午10.54.50.png

有住宅,工作,其他,但都是傳真。其實(shí)是在通用下面,因?yàn)猷]箱也用得到這些標(biāo)簽。


屏幕快照 2016-04-07 下午10.56.56.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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