Sami
Sami

Reputation: 587

Getting date a contact was added to the address book in Swift 2, iOS 9

In the previous version of Swift, I was able to get the date a contact was created in the phone using the kABPersonCreationDateProperty which came from the AddressBook framework.

Now that Apple has migrated to the Contacts framework, I don't see a way to retrieve the date a contact was created in their Documentation

How can I retrieve the date a contact was created in Swift 2? Sample code would be much appreciated :)

Upvotes: 6

Views: 1381

Answers (1)

KFDoom
KFDoom

Reputation: 634

I know this is old, but just in case anyone needs it, I used the old fashioned way. There's no obvious way to record the date a contact is created but this technique circumvents the framework initially, in order to retrieve with the current locale time with NSDate then converting it to a String, and finally assigning to to CNContactDatesKey --which is the only available CNContactsLabel that categorically allows for non-birthday/anniversary dates assignment.

        let exampleContact = CNContact()

        let currentDate = NSDate()
        let dateFormatter = NSDateFormatter()
        dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
        let finalConvertedDate = dateFormatter.stringFromDate(currentDate)
        exampleContact.setValue(finalConvertedDate, forKey: CNContactDatesKey)

...Then later, when you need to request it, you'd begin with the below process:

        let request = CNContactFetchRequest(keysToFetch: [CNContactIdentifierKey, CNContactDatesKey])

Any input is welcomed.

Upvotes: 1

Related Questions