We are not allowed to store the files persistent but allow the customer to decide himself if he want to save it in a different app/send it somewhere. Sounds like a job for UIDocumentInteractionController
but without the persistence part.
Inside of the docs Apple states:
"[…]you can use a document interaction controller with a file that was synced to your app’s Documents/Shared directory."
Is it possible to open a file directly without storing it as a file inside of a UIDocumentInteractionController
?
We already have the PDF / Image as Data. Is there a way to encode Data to an URL and use that to open a Interaction controller?
This cannot be done, you can only share local resources.
What you could do is to save the file in a temp folder and the access it. Below is the code of how you can achieve this. Checkout the comments for description:
import UIKit
class ViewController: UIViewController {
let sharingController = UIDocumentInteractionController()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let pdfURL = Bundle.main.url(forResource: "yourPDF", withExtension: "pdf") {
do {
let pdfData = try Data(contentsOf: pdfURL)
// create a destination url to save your pdf data
let destURL = FileManager.default.temporaryDirectory.appendingPathComponent("tempFile.pdf")
// write your pdf data to the destinastion url
do {
try pdfData.write(to: destURL)
// share your url using UIDocumentInteractionController
shareURL(url: destURL)
} catch {
print(error)
}
} catch {
print(error)
}
}
}
func shareURL(url: URL){
sharingController.url = url
sharingController.uti = "public.data, public.content"
sharingController.name = url.lastPathComponent
sharingController.presentOptionsMenu(from: view.frame, in: view, animated: true)
}
}
There is also no need to delete the file since it´s in the temporary directory.