将 CNContact 作为 vCard 发送电子邮件

Email CNContact as vCard

是否可以通过电子邮件将 CNMutableContact 转换为 vCard? 通过下面的代码,我正在创建 CNMutableContact 并将其保存到文档目录,但是否可以直接发送而不是保存它?我不需要保存它。

let contact = CNMutableContact()
    contact.givenName = "John"
    contact.familyName = "Doe"
    contact.emailAddresses = [
        CNLabeledValue(label: CNLabelWork, value: "john.doe@abc.com")
    ]
    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])
    }

尝试使用以下代码在电子邮件中将联系人作为 vcard 发送。

let contact = CNMutableContact()
   contact.givenName = "John"
   contact.familyName = "Doe"
   contact.emailAddresses = [
       CNLabeledValue(label: CNLabelWork, value: "john.doe@abc.com")
   ]
   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)