美文网首页
ABRecord / ABPerson / ABAddressB

ABRecord / ABPerson / ABAddressB

作者: 小凡凡520 | 来源:发表于2019-10-08 09:21 被阅读0次
    一、ABRecord 联系人信息
    • 获取信息
    // 信息 id,具有唯一性
    ABRecordGetRecordID(<#T##record: ABRecord!##ABRecord!#>)
    // 信息 类型,可以是 kABPersonType 和 kABGroupType
    ABRecordGetRecordType(<#T##record: ABRecord!##ABRecord!#>)
    // 获取信息内部某条数据
    ABRecordCopyValue(<#T##record: ABRecord!##ABRecord!#>, <#T##property: ABPropertyID##ABPropertyID#>)
    // 全名
    ABRecordCopyCompositeName(<#T##record: ABRecord!##ABRecord!#>)
    
    • 内部信息操作
    // 修改信息
    ABRecordSetValue(<#T##record: ABRecord!##ABRecord!#>, kABPersonFirstNameProperty, <#T##value: CFTypeRef!##CFTypeRef!#>, <#T##error: UnsafeMutablePointer<Unmanaged<CFError>?>!##UnsafeMutablePointer<Unmanaged<CFError>?>!#>)
    // 移除信息
    ABRecordRemoveValue(<#T##record: ABRecord!##ABRecord!#>, kABPersonFirstNameProperty, <#T##error: UnsafeMutablePointer<Unmanaged<CFError>?>!##UnsafeMutablePointer<Unmanaged<CFError>?>!#>)
    
    二、ABPerson
    • 创建 联系人 记录
    guard let aRecordRef = ABPersonCreate() else {
        return
    }
    ABPersonCreateInSource(aRecordRef as ABRecord)
    ABPersonCopySource(aRecordRef as ABRecord)
    
    • 获取联系人 记录
    guard let aRecordRef = ABPersonCreate() else {
        return
    }
    ABPersonCreateInSource(aRecordRef as ABRecord)
    ABPersonCopySource(aRecordRef as ABRecord)
    
    if let addressBook = ABAddressBookCreate() {
        // 获取所有的联系人数量
        ABAddressBookGetPersonCount(addressBook as ABAddressBook)
        // 根据 ID 获取联系人 记录
        ABAddressBookGetPersonWithRecordID(addressBook as ABAddressBook, 123)
        // 获取所有联系人 记录
        ABAddressBookCopyArrayOfAllPeople(addressBook as ABAddressBook)
        // 新排序 的所有 联系人 记录
        ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook as ABAddressBook, aRecordRef as ABRecord, ABPersonSortOrdering(kABPersonSortByFirstName))
        // 根据 名 搜索联系人
        ABAddressBookCopyPeopleWithName(addressBook as ABAddressBook, "123" as CFString)
    }
    
    • 详细联系人 记录
    if let addressBook = ABAddressBookCreate() {
        // 获取所有联系人 记录
        let peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook as ABAddressBook)
        // 根据上面获取的联系人 记录数组获取某个联系人
        if let person = CFArrayGetValueAtIndex(peopleArray as! CFArray, 0) as? ABRecord {
            let lastName = ABRecordCopyValue(person, kABPersonLastNameProperty)
            if let phones = ABRecordCopyValue(person, kABPersonPhoneProperty) as? ABMultiValue {
                let phoneCount = ABMultiValueGetCount(phones)
                for i in 0 ..< phoneCount {
                    let phoneLabel = ABMultiValueCopyLabelAtIndex(phones, i)
                    let phoneValue = ABMultiValueCopyValueAtIndex(phones, i)
                }
            }
        }
    }
    
    • 联系人 头像 处理
    if let addressBook = ABAddressBookCreate() {
        // 获取所有联系人 记录
        let peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook as ABAddressBook)
        // 根据上面获取的联系人 记录数组获取某个联系人
        if let person = CFArrayGetValueAtIndex(peopleArray as! CFArray, 0) as? ABRecord {
            if ABPersonHasImageData(person) {
                let head = ABPersonCopyImageData(person)
                let headOption = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)
                if ABPersonRemoveImageData(person, nil) {
                    // 移除头像成功
                }
                if ABPersonSetImageData(person, headOption as! CFData, nil) {
                    // 设置头像成功
                }
            }
        }
    }
    
    • 生成名片
    if let addressBook = ABAddressBookCreate() {
        // 获取所有联系人 记录
        let peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook as ABAddressBook)
        // 联系人记录 转 名片数据
        let cd = ABPersonCreateVCardRepresentationWithPeople(peopleArray as! CFArray)
        let ca = ABPersonCreatePeopleInSourceWithVCardRepresentation(nil, cd as! CFData)
    }
    
    • 保存地址簿
    if let addressBook = ABAddressBookCreate() {
        ABAddressBookSave(addressBook as ABAddressBook, nil)
    }
    
    • 添加/删除
    if let addressBook = ABAddressBookCreate() {
        // 获取所有联系人 记录
        let peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook as ABAddressBook)
        // 根据上面获取的联系人 记录数组获取某个联系人
        if let person = CFArrayGetValueAtIndex(peopleArray as! CFArray, 0) as? ABRecord {        
            ABAddressBookAddRecord(addressBook as ABAddressBook, person, nil)
            ABAddressBookRemoveRecord(addressBook as ABAddressBook, person, nil)
        }
    }
    

    相关文章

      网友评论

          本文标题:ABRecord / ABPerson / ABAddressB

          本文链接:https://www.haomeiwen.com/subject/mgnqjqtx.html