Sharing a text file using the UIActivityViewController

I am trying to transfer a text file using the UIActivityViewController. I create and write to my file in my application, and then allow the user to share this file using any means that they would like.

Currently, I can access the file as follows:

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 splits a long line of file content. Instead, I would like to share the file. How can I do this with Swift?

+6
source share
1 answer

After a short search, I came up with a solution, it turned out that instead of the actual file, I should share this URL. I replaced

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

with

 let activityItem:NSURL = NSURL(fileURLWithPath:path) 

and it works as expected!

+6
source

All Articles