如何在 mac OS 上使用 CNContactViewController 编辑联系人

How to edit a contact using CNContactViewController on mac OS

我想创建一个允许用户编辑联系人的应用程序。

根据 the docs the CNContactViewController has the property <a href="https://developer.apple.com/reference/contactsui/cncontactviewcontroller/1616920-allowsediting" rel="nofollow noreferrer">allowsEditing</a> iOS 和 macOS 10.11+

在Xcode中CNContactViewController只有一个属性并且没有联系具体方法:

@NSCopying open var contact: CNContact?

是否可以使用 ContactsUI 框架在 mac 上编辑联系人,或者这是文档中的错误?

我是这样显示联系人的:

if let vc = segue.destinationController as? CNContactViewController{

    let contact = CNMutableContact()
    contact.givenName = "John"
    contact.familyName = "Appleseed"

    let homeEmail = CNLabeledValue(label:CNLabelHome, value:"john@example.com" as NSString)
    let workEmail = CNLabeledValue(label:CNLabelWork, value:"j.appleseed@icloud.com" as NSString)
    contact.emailAddresses = [homeEmail, workEmail]

    contact.phoneNumbers = [CNLabeledValue(
        label:CNLabelPhoneNumberiPhone,
        value:CNPhoneNumber(stringValue:"(408) 555-0126"))]

    let homeAddress = CNMutablePostalAddress()
    homeAddress.street = "1 Infinite Loop"
    homeAddress.city = "Cupertino"
    homeAddress.state = "CA"
    homeAddress.postalCode = "95014"
    contact.postalAddresses = [CNLabeledValue(label:CNLabelHome, value:homeAddress)]

    var birthday = DateComponents()
    birthday.day = 1
    birthday.month = 4
    birthday.year = 1988  // You can omit the year value for a yearless birthday
    contact.birthday = birthday
    vc.contact = contact
}

但是,没有可用的编辑按钮:

我在 Sierra 下使用我的开发者信用之一向 Apple 询问此问题,得到的答复是 'edit' 功能尚未在 macOS 下实现。有人建议我提交一份雷达(我提交了)。我希望它会针对 High Sierra 进行更新,但遗憾的是,还没有这样的 API。

如果您单击实际的 'allowsEditing' link,您将看到此 API,它仅在 iOS 下(目前)。可惜 - 我真的很期待删除通讯录代码的地址簿代码,但我现在还不能。