获取联系人添加到地址簿的日期 Swift 2,iOS 9
Getting date a contact was added to the address book in Swift 2, iOS 9
在 Swift 的先前版本中,我能够使用来自 AddressBook
的 kABPersonCreationDateProperty
获取在 phone 中创建联系人的日期框架。
现在 Apple 已经迁移到 Contacts
框架,我看不到在他们的 Documentation
中检索联系人创建日期的方法
如何检索在 Swift 2 中创建联系人的日期?示例代码将不胜感激:)
我知道这是旧的,但为了以防万一有人需要它,我使用了老式的方式。没有明显的方法来记录联系人的创建日期,但这种技术最初绕过了框架,以便使用 NSDate
检索当前语言环境时间,然后将其转换为字符串,最后分配给 CNContactDatesKey
-- 这是唯一明确允许 non-birthday/anniversary 日期分配的可用 CNContactsLabel。
let exampleContact = CNContact()
let currentDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
let finalConvertedDate = dateFormatter.stringFromDate(currentDate)
exampleContact.setValue(finalConvertedDate, forKey: CNContactDatesKey)
...然后,当您需要请求它时,您将从以下流程开始:
let request = CNContactFetchRequest(keysToFetch: [CNContactIdentifierKey, CNContactDatesKey])
欢迎任何意见。
在 Swift 的先前版本中,我能够使用来自 AddressBook
的 kABPersonCreationDateProperty
获取在 phone 中创建联系人的日期框架。
现在 Apple 已经迁移到 Contacts
框架,我看不到在他们的 Documentation
如何检索在 Swift 2 中创建联系人的日期?示例代码将不胜感激:)
我知道这是旧的,但为了以防万一有人需要它,我使用了老式的方式。没有明显的方法来记录联系人的创建日期,但这种技术最初绕过了框架,以便使用 NSDate
检索当前语言环境时间,然后将其转换为字符串,最后分配给 CNContactDatesKey
-- 这是唯一明确允许 non-birthday/anniversary 日期分配的可用 CNContactsLabel。
let exampleContact = CNContact()
let currentDate = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.FullStyle
let finalConvertedDate = dateFormatter.stringFromDate(currentDate)
exampleContact.setValue(finalConvertedDate, forKey: CNContactDatesKey)
...然后,当您需要请求它时,您将从以下流程开始:
let request = CNContactFetchRequest(keysToFetch: [CNContactIdentifierKey, CNContactDatesKey])
欢迎任何意见。