Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing a text file with UIActivityViewController

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?

like image 382
samturner Avatar asked Jul 07 '15 01:07

samturner


1 Answers

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!

like image 199
samturner Avatar answered Oct 23 '22 08:10

samturner