Replacement of unifiedMeContactWithKeys?

I'm trying to fetch Me contact (if it's defined) from CNContact store.

The only mention I found about doing this is through unifiedMeContactWithKeys(toFetch:).

But when I try to use it, both in Xcode Version 8.1 (8B62) and Version 8.2 beta (8C23), and error shows indicating it's unavailable.

Xcode's documentation mentions the method, but has no further documentation whatsoever.


The lines of code used is:


do {
    let meContact = try store.unifiedMeContactWithKeys(toFetch: keysToFetch as! [CNKeyDescriptor])
    self.me = Persona(nombre: "\(meContact.givenName)\(meContact.middleName != "" ? " " + meContact.middleName : "")",                 
    apellido: "\(meContact.familyName)",                 
    fechaDeNacimiento: formatter.date(from: "\(meContact.birthday!.day!) \(meContact.birthday!.month!) \(meContact.birthday!.year!)")! ,
    direccion: 8537)
   }
catch {
        self.me = Persona(nombre: "John", apellido: "Doe", fechaDeNacimiento: formatter.date(from: "01 12 1962")!, direccion: 8537)
      }

Accepted Reply

Are you developing for iOS? This API is only available on macOS.


If you are developing for macOS, the generated Swift interface file to CNContactStore includes a header doc comment for the API, but this won't show up in the generated interface if you are developing on iOS because it is not available. You can pull up the generated interface file by holding down the Command key while clicking on the CNContactStore initializer in your code. Here's the header doc:


/!
     * @abstract Fetch the unified contact that is the "me" card.
     *
     * @discussion Fetches the contact that is represented in the user interface as "My Card".
     *
     * @param keys The properties to fetch into the returned CNContact object. Should only fetch the properties that will be used. Can combine contact keys and contact key descriptors.
     * @param error If an error occurs, contains error information.
     * @return The unified contact that is the "me" card. If no "me" card is set, nil is returned.
     */
    @available(OSX 10.11, *)
    open func unifiedMeContactWithKeys(toFetch keys: [CNKeyDescriptor]) throws -> CNContact

Replies

Are you developing for iOS? This API is only available on macOS.


If you are developing for macOS, the generated Swift interface file to CNContactStore includes a header doc comment for the API, but this won't show up in the generated interface if you are developing on iOS because it is not available. You can pull up the generated interface file by holding down the Command key while clicking on the CNContactStore initializer in your code. Here's the header doc:


/!
     * @abstract Fetch the unified contact that is the "me" card.
     *
     * @discussion Fetches the contact that is represented in the user interface as "My Card".
     *
     * @param keys The properties to fetch into the returned CNContact object. Should only fetch the properties that will be used. Can combine contact keys and contact key descriptors.
     * @param error If an error occurs, contains error information.
     * @return The unified contact that is the "me" card. If no "me" card is set, nil is returned.
     */
    @available(OSX 10.11, *)
    open func unifiedMeContactWithKeys(toFetch keys: [CNKeyDescriptor]) throws -> CNContact

Hi Ed, thanks for the reply. I'm indeed developing for iOS.

So I then assume there's no way to get a Me card, even if it's available and recognized in the iOS Contacts app...

That's correct.