Monday, 30 December 2019

While sharing a vcf file in iOS , the file is not getting attached in mail using swift

I'm trying to share a vcf file using UIActivityViewController. It shares the file with all other options except mail. It just opens the mail composer without any attachment. Here's my code:

guard let directoryURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first else {
        return
        }


        var filename = NSUUID().uuidString



        if let fullname = CNContactFormatter().string(from: contact) {
            filename = fullname.components(separatedBy: " ").joined(separator: " ")
        }

        let fileURL = directoryURL
        .appendingPathComponent(filename)
        .appendingPathExtension("vcf")

        do{
            let data = try CNContactVCardSerialization.data(with: [contact])

            print("filename: \(filename)")
            print("contact: \(String(describing: String(data: data, encoding: String.Encoding.utf8)))")

            try data.write(to: fileURL, options: [.atomicWrite])
        }
        catch{
            print(error.localizedDescription)
        }

        let activityViewController = UIActivityViewController(
            activityItems: [fileURL],
        applicationActivities: nil
        )

        present(activityViewController, animated: true, completion: nil)

I want to attach this contact as a vcf file in mail app when user selects mail option for sharing.



from While sharing a vcf file in iOS , the file is not getting attached in mail using swift

No comments:

Post a Comment