Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过CNMutableContact在ios mobile contacts中添加自定义隐藏字段?_Ios_Swift_Contacts_Cncontact - Fatal编程技术网

如何通过CNMutableContact在ios mobile contacts中添加自定义隐藏字段?

如何通过CNMutableContact在ios mobile contacts中添加自定义隐藏字段?,ios,swift,contacts,cncontact,Ios,Swift,Contacts,Cncontact,我想从我的应用程序在iOS mobile contacts中添加一个自定义隐藏字段 是否可以在iOS mobile contacts中添加具有值的新隐藏字段?CNMutableContact允许我添加新的自定义属性吗?很遗憾,简短的回答是“不”,这是不可能的 答案很长:CNMutableContact是子类化CNContact,它附带以下公共接口 open class CNContact : NSObject, NSCopying, NSMutableCopying, NSSecureCodin

我想从我的应用程序在iOS mobile contacts中添加一个自定义隐藏字段

是否可以在iOS mobile contacts中添加具有值的新隐藏字段?CNMutableContact允许我添加新的自定义属性吗?

很遗憾,简短的回答是“不”,这是不可能的

答案很长:
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 */
}

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

W文档似乎表明你可以-我也在搜索相同的答案。你能解释一下隐藏字段吗?什么样的隐藏字段不,你不能在CNContact或CNMutableContact中添加新字段似乎相关,但日期:不是我们想要的ThoughThank@cr0ss我喜欢你在这里共享公共界面的方式。这很有帮助。重新指定字段的用途听起来像是一种变通方法,但最终用户仍然可以看到。倒霉蛋。:)无论如何,非常感谢!谢谢你的回答@cr0ssI我在这里尝试按照苹果的指导方针工作。目前似乎没有任何替代方法,但使用现有字段的风险在于,用户可能决定将该字段重新用于Contacts.app中的其他内容,并践踏您的字段数据。
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?
}