Sharing a text file with UIActivityViewController

i0S Swift Issue

Question or problem in the Swift programming language:

I’m attempting to share a text file with a UIActivityViewController. I’m creating and writing to my file in my app, and then allowing the user to share this file using whatever means they would like.

Currently I can access the file like this:

let text2 = String(contentsOfFile: path, encoding: NSUTF8StringEncoding)

let objectsToShare = [text2]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)

Which works well, but it shares a long string of the contents of the file. Instead, I would like to share the file itself. How can I do this with Swift?

How to solve the problem:

After a little more searching I came up with the solution, turns out that instead of the actual file, I should have been trying to share the URL. I replaced

let text2 = String(contentsOfFile:path, encoding: NSUTF8StringEncoding)

with

let activityItem:NSURL = NSURL(fileURLWithPath:path)

and it works as expected!

Hope this helps!