Reputation: 2024
Is it possible to convert CNMutableContact
as an vCard by email ?
By below code I'm creating CNMutableContact and saving it to document directory but is it possible to send it directly instead of saving it? I don't need to save that.
let contact = CNMutableContact()
contact.givenName = "John"
contact.familyName = "Doe"
contact.emailAddresses = [
CNLabeledValue(label: CNLabelWork, value: "[email protected]")
]
let data = try! CNContactVCardSerialization.data(with: [contact])
let s = String(data: data, encoding: String.Encoding.utf8)
if let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first{
let fileURL = directoryURL.appendingPathComponent("john.doe").appendingPathExtension("vcf")
try! data.write(to: fileURL, options: [.atomicWrite])
}
Upvotes: 0
Views: 484
Reputation: 250
Try Below Code for send contact as vcard in Email.
let contact = CNMutableContact()
contact.givenName = "John"
contact.familyName = "Doe"
contact.emailAddresses = [
CNLabeledValue(label: CNLabelWork, value: "[email protected]")
]
let data = try! CNContactVCardSerialization.data(with: [contact])
let mailComposer: MFMailComposeViewController = MFMailComposeViewController()
mailComposer.mailComposeDelegate = self
mailComposer.setSubject("emailsubject".localize())
// mailComposer.setMessageBody("", isHTML: false)
mailComposer.addAttachmentData(data), mimeType: "text/vcf", fileName: "contacts.vcf")
self.present(mailComposer, animated: true, completion: nil)
Upvotes: 1