When the user is confronted with an unexpected error, I'm providing them with the option to send the error log contents as an email body of an NSSharingService item as such:
let errorLog = "[Extensive log output with many symbols and new lines]"
let service = NSSharingService(named: NSSharingService.Name.composeEmail)
service?.recipients = ["[email protected]"]
service?.subject = "Help: App Error"
service?.perform(withItems: [errorLog])
Is there an efficient way to go about sending the error log contents as an attached text file, using temporary file references; as opposed to having to work with directories and permissions? Something along the lines of:
let txtFile = String(errorLog) as? file(name: "ErrorLog", withExtension: "txt")
service?.perform(withItems: [txtFile])
Swift has constantly surprised me with how simple and easy some of its implementation can be, so I thought I'd ask.
Thank you!
Swift 5.2
Updated: September 4, 2022
Using the solution found here, I was able to create a String extension using
FileManager.default.temporary:Which can be called from any String; ie.
Conclusion: NSSharingService
All together, this will end up looking something like: