Swift 4.2 and Swift 5
If you already have a file in the directory and want to share it, just add it to activityItems
:
let fileURL = NSURL(fileURLWithPath: "The path where the file you want to share is located") // Create the Array which includes the files you want to share var filesToShare = [Any]() // Add the path of the file to the Array filesToShare.append(fileURL) // Make the activityViewContoller which shows the share-view let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil) // Show the share-view self.present(activityViewController, animated: true, completion: nil)
If you need to create a file :
I use this extension to create files from Data
(read the comments in the code to explain how this works):
As in typedef answer, get the current document directory:
/// Get the current directory /// /// - Returns: the Current directory in NSURL func getDocumentsDirectory() -> NSString { let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) let documentsDirectory = paths[0] return documentsDirectory as NSString }
Extension for Data
:
extension Data {
Examples of use :
Share Image Files :
in png file
// Convert the image into png image data let pngImageData = yourImage.pngData() // Write the png image into a filepath and return the filepath in NSURL let pngImageURL = pngImageData?.dataToFile(fileName: "nameOfYourImageFile.png") // Create the Array which includes the files you want to share var filesToShare = [Any]() // Add the path of png image to the Array filesToShare.append(pngImageURL!) // Make the activityViewContoller which shows the share-view let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil) // Show the share-view self.present(activityViewController, animated: true, completion: nil)
in jpg file
// Convert the image into jpeg image data. compressionQuality is the quality-compression ratio in % (from 0.0 (0%) to 1.0 (100%)); 1 is the best quality but have bigger filesize let jpgImageData = yourImage.jpegData(compressionQuality: 1.0) // Write the jpg image into a filepath and return the filepath in NSURL let jpgImageURL = jpgImageData?.dataToFile(fileName: "nameOfYourImageFile.jpg") // Create the Array which includes the files you want to share var filesToShare = [Any]() // Add the path of jpg image to the Array filesToShare.append(jpgImageURL!) // Make the activityViewContoller which shows the share-view let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil) // Show the share-view self.present(activityViewController, animated: true, completion: nil)
Share text files :
Other files :
You can make a file from any file in the Data
format, and as far as I know, almost everything in Swift can be converted to Data
, for example String
, Int
, Double
, Any
...:
// the Data you want to share as a file let data = Data() // Write the data into a filepath and return the filepath in NSURL // Change the file-extension to specify the filetype (.txt, .json, .pdf, .png, .jpg, .tiff...) let fileURL = data.dataToFile(fileName: "nameOfYourFile.extension") // Create the Array which includes the files you want to share var filesToShare = [Any]() // Add the path of the file to the Array filesToShare.append(fileURL!) // Make the activityViewContoller which shows the share-view let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil) // Show the share-view self.present(activityViewController, animated: true, completion: nil)