Swift grab "My card" CNContact

I can’t figure out how to capture My Map users from my contacts. I am developing my own Mac application using swift.

+4
source share
2 answers

This is not from (completely new, like MacOS 10.11) CNContact, but MacOS. The ABAddressBook framework has a method calledme() that will return the registered user account of ABPerson.

And to get the equivalent of vCard, call vCardRepresentation()ABPerson on this object .

, MacOS (, MacOS 10.9, 10.10).

API unifiedMeContactWithKeysToFetch: CNContactStore, .h SDK, CNContactStore.

0

CNContact api, macOS 10.11+, iOS .

( iOS ABAddressBook , me() MacOS, MacOS 10.2+.)

import Contacts

let nameKeys = [
    CNContactNamePrefixKey,
    CNContactGivenNameKey,
    CNContactMiddleNameKey,
    CNContactFamilyNameKey,
    CNContactNameSuffixKey,
    ] as [CNKeyDescriptor]

do {
    let contactStore = CNContactStore()
    let me = try contactStore.unifiedMeContactWithKeys(toFetch: nameKeys)
} catch let error {
    print("Failed to retreive Me contact: \(error)")
}

, , :

let allContactKeys = [
    CNContactNamePrefixKey,
    CNContactGivenNameKey,
    CNContactMiddleNameKey,
    CNContactFamilyNameKey,
    CNContactNameSuffixKey,
    CNContactOrganizationNameKey,
    CNContactDepartmentNameKey,
    CNContactJobTitleKey,
    CNContactBirthdayKey,
    CNContactNicknameKey,
    CNContactNoteKey,
    CNContactNonGregorianBirthdayKey,
    CNContactPreviousFamilyNameKey,
    CNContactPhoneticGivenNameKey,
    CNContactPhoneticMiddleNameKey,
    CNContactPhoneticFamilyNameKey,
    CNContactImageDataKey,
    CNContactThumbnailImageDataKey,
    CNContactImageDataAvailableKey,
    CNContactTypeKey,
    CNContactPhoneNumbersKey,
    CNContactEmailAddressesKey,
    CNContactPostalAddressesKey,
    CNContactDatesKey,
    CNContactUrlAddressesKey,
    CNContactRelationsKey,
    CNContactSocialProfilesKey,
    CNContactInstantMessageAddressesKey,
    ] as [CNKeyDescriptor]
0

Source: https://habr.com/ru/post/1612840/


All Articles