如何通过CNMutableContact在ios移动联系人中添加自定义隐藏字段?

时间:2016-10-17 06:43:49

标签: ios swift contacts cncontact

我想在我的应用程序的iOS移动联系人中添加一个自定义隐藏字段。

是否可以在iOS移动联系人中添加具有值的新隐藏字段? CNMutableContact是否允许我添加新的自定义属性?

1 个答案:

答案 0 :(得分:2)

简短的回答很遗憾不,那是不可能的

答案很长:CNMutableContact是子类化CNContact,它带有以下公共接口。

open class CNContact : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
    open var identifier: String { get }
    open var contactType: CNContactType { get }
    open var namePrefix: String { get }
    open var givenName: String { get }
    open var middleName: String { get }
    open var familyName: String { get }
    open var previousFamilyName: String { get }
    open var nameSuffix: String { get }
    open var nickname: String { get }
    open var organizationName: String { get }
    open var departmentName: String { get }
    open var jobTitle: String { get }
    open var phoneticGivenName: String { get }
    open var phoneticMiddleName: String { get }
    open var phoneticFamilyName: String { get }
    open var phoneticOrganizationName: String { get }
    open var note: String { get }
    open var imageData: Data? { get }
    open var thumbnailImageData: Data? { get }
    open var imageDataAvailable: Bool { get }
    open var phoneNumbers: [CNLabeledValue<CNPhoneNumber>] { get }
    open var emailAddresses: [CNLabeledValue<NSString>] { get }
    open var postalAddresses: [CNLabeledValue<CNPostalAddress>] { get }
    open var urlAddresses: [CNLabeledValue<NSString>] { get }
    open var contactRelations: [CNLabeledValue<CNContactRelation>] { get }
    open var socialProfiles: [CNLabeledValue<CNSocialProfile>] { get }
    open var instantMessageAddresses: [CNLabeledValue<CNInstantMessageAddress>] { get }
    open var birthday: DateComponents? { get }
    open var nonGregorianBirthday: DateComponents? { get }
    open var dates: [CNLabeledValue<NSDateComponents>] { get }
    /* [...] functions */
}

这两种类型之间唯一必要的差异(使其变得可变)是,除了identifier属性之外,CNMutableContact的属性未被指定为仅限get&#39 ; S。仔细观察后,您现在可以看到,Contact对象上不可能有自定义属性。像我在以下示例中所做的那样对CNMutableContact进行子类化将导致 nilError 并且CNContactStore不会存储我们的自定义联系人。

func saveCustomContact() {
    let contactStore = CNContactStore()
    let contact = MyContact()

    contact.givenName = "John"
    contact.familyName = "Doe"
    contact.test = "Hello World"

    do {
        let saveRequest = CNSaveRequest()
        saveRequest.add(contact, toContainerWithIdentifier: nil)
        try contactStore.execute(saveRequest)
    } catch {
        print(error)
    }
}

func retrieveCustomContact() {
    DispatchQueue.global().async {
        let keysToFetch = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName),CNContactPhoneNumbersKey] as [Any]
        let fetchRequest = CNContactFetchRequest( keysToFetch: keysToFetch as! [CNKeyDescriptor])
        CNContact.localizedString(forKey: CNLabelPhoneNumberiPhone)

        fetchRequest.mutableObjects = false
        fetchRequest.unifyResults = true
        fetchRequest.sortOrder = .userDefault

        do {
            try CNContactStore().enumerateContacts(with: fetchRequest) { (contact, stop) -> Void in
                guard let contact = contact as? MyContact else { print("damn - it's not working!"); return }
                print(contact.test)
            }
        } catch {
            print(error)
        }
    }
}

open class MyContact: CNMutableContact {
    open var test: String?
}

这让我得出结论,苹果并不希望我们将自定义字段存储在默认联系人的书中,从同步(序列化/反序列化)的角度来看,这很容易理解。