CNContact
이름, 이메일, 생일로 연락처 생성
@objc func createContact() {
let newContact = CNMutableContact()
newContact.givenName = nameText.text!
if let homeEmail = emailText.text {
let homeEmail = CNLabeledValue(label: CNLabelHome, value: homeEmail as NSString)
newContact.emailAddresses = [homeEmail]
}
let birthdayComponents = Calendar.current.dateComponents([Calendar.Component.year, Calendar.Component.month, Calendar.Component.day], from: datePicker.date)
newContact.birthday = birthdayComponents
do {
let saveRequest = CNSaveRequest()
saveRequest.add(newContact, toContainerWithIdentifier: nil)
try AppDelegate.appDelegate.contactStore.execute(saveRequest)
navigationController?.popViewController(animated: true)
dismiss(animated: true, completion: nil)
} catch {
Helper.show(message: "Unable to save the new contact.")
}
}
연락처 내의 지정 키 내용 보이기
let contactPickerViewController = CNContactPickerViewController()
contactPickerViewController.predicateForEnablingContact = NSPredicate(format: "Birthday != nil")
contactPickerViewController.delegate = self
contactPickerViewController.displayedPropertyKeys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey]