In my app, I display some remote images or PDF files and want to give the user the ability to download them. In order to do so, I try to save them locally first in .documentDirectory before opening a UIDocumentInteractionController to handle the file.
However, I am having an issue, which is that even if the action sheet opens fine and proposes all the expected options, in the end I can never use the file because of an error. Specifically:
If I try to use the file in a mail, the mail opens but empty,
If I try to use it in Whatsapp, I get an error saying "The item cannot be shared. Please selected a different item."
And if I choose "Save to Files", the files action sheet briefly opens but closes immediately afterwards with an error in the console saying: [ShareSheet] cancelled request - error: The operation couldn’t be completed. Invalid argument
Here is the code I use to cache the remote file, then to open it with UIDocumentInteractionController:
URLSession.shared.downloadTask(with: url) { localUrl, response, error in
if let localUrl = localUrl {
do {
let imageData = try Data(contentsOf: localUrl)
let d = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last
if let docUrl = d?.appendingPathComponent(url.lastPathComponent) {
try imageData.write(to: docUrl)
self.download(docUrl)
}
} catch {
print("Oops: \(error)")
}
}
}.resume()
func download(_ url: URL) {
DispatchQueue.main.async {
let documentInteractionController = UIDocumentInteractionController()
documentInteractionController.delegate = self
documentInteractionController.url = url
documentInteractionController.uti = url.typeIdentifier ?? "public.data, public.content"
documentInteractionController.name = url.localizedName ?? url.lastPathComponent
documentInteractionController.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
}
}
Thank you for your help
I can't tell you why it doesn't work with a UIDocumentInteractionController, but it does work with a UIActivityViewController.
private func download(_ url: URL)
{
DispatchQueue.main.async {
let avc = UIActivityViewController(activityItems: [url], applicationActivities: nil)
self.present(avc, animated: true, completion: nil)
}
}
Related
I am getting getting multiple PDF urls from server response and showing them in tableview with download option for each cell.
I am able to download each pdf file only once, But, Tried to second time download, It is showing already downloaded error.
How to fix this?
Here is my code
func downloadButtonTapped(index: Int) {
let finalUrlStr = "(dataResponse?[index].brochure)")
let fileURL = URL(string: finalUrlStr)
let fileName = String((fileURL!.lastPathComponent)) as NSString
// Create destination URL
let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationFileUrl = documentsUrl.appendingPathComponent("\(fileName)")
//Create URL to the source file you want to download
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileURL!)
LoadingView.show()
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
do {
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
do {
//Show UIActivityViewController to save the downloaded file
let contents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
for indexx in 0..<contents.count {
if contents[indexx].lastPathComponent == destinationFileUrl.lastPathComponent {
let activityViewController = UIActivityViewController(activityItems: [contents[indexx]], applicationActivities: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
self.present(activityViewController, animated: true, completion: nil)
LoadingView.hide()
}
}
}
}
catch (let err) {
// DispatchQueue.main.async {
print("error: \(err.localizedDescription)")
LoadingView.hide()
self.showBasicAlert(title: "\(err.localizedDescription)", message: "")
// }
}
} catch (let writeError) {
print("Error creating a file \(destinationFileUrl) : \(writeError)")
LoadingView.hide()
self.showBasicAlert(title: "\(writeError.localizedDescription)", message: "")
}
} else {
print("Error took place while downloading a file. Error description: \(error?.localizedDescription ?? "")")
self.showBasicAlert(title: "\(error?.localizedDescription ?? "")", message: "")
}
}
task.resume()
}
Even though, PDF not downloaded even one time before download file, activitycontroller displaying and If we close that without download/save file, Again trying to download, Same error message showing like already exist file
How to download multiple times like whenever user taps on download option, It should download the pdf file.
Also after downloaded pdf, I need to show open pdf from external not inside app
Any suggestions?
I have fixed it by myself by removing condition.
Now I am able to download whenever user tap on download button.
Here is the code.
func downloadButtonTapped(index: Int) {
// print("index \(index)")
let finalUrlStr = "(dataResponse?[index].brochure)")
let fileURL = URL(string: finalUrlStr)
if let fileUrl = fileURL {
let fileName = String((fileUrl.lastPathComponent)) as NSString
// Create destination URL
let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationFileUrl = documentsUrl.appendingPathComponent("\(fileName)")
//Create URL to the source file you want to download
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileUrl)
LoadingView.show()
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if error == nil {
// Success
if let statusCode = (response as? HTTPURLResponse)?.statusCode {
print("Successfully downloaded. Status code: \(statusCode)")
}
do {
//Show UIActivityViewController to save the downloaded file
let contents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
for indexx in 0..<contents.count {
if contents[indexx].lastPathComponent == destinationFileUrl.lastPathComponent {
let activityViewController = UIActivityViewController(activityItems: [contents[indexx]], applicationActivities: nil)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
self.present(activityViewController, animated: true, completion: nil)
LoadingView.hide()
}
}
}
}
catch (let err) {
print("error: \(err.localizedDescription)")
DispatchQueue.main.async {
LoadingView.hide()
self.showBasicAlert(title: "\(err.localizedDescription)", message: "")
}
}
}
}
task.resume()
}
}
Problem with downloading files.
I'm trying to download a file form url and push it into open/share modal. But da data downloads as Data and if I try saving it to File app it saves a file called Data.
I just need to download and share the file with the original extension. Like file.extension.
Here's the code use. I used Alamofire pod here:
AF.download(url).responseData { response in
if let preset = response.value {
let shareArray = [preset]
let activityViewController = UIActivityViewController(activityItems: shareArray , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
}
Also tried this code but the app crashed:
if let url = URL(string: downloadURL!) {
let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in
if let localURL = localURL {
let shareArray = [localURL]
let activityViewController = UIActivityViewController(activityItems: shareArray , applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view
self.present(activityViewController, animated: true, completion: nil)
}
}
task.resume()
}
The issue there is that you are trying to share the temporary file returned. It has a dynamic UTI (Unified Type Identifier). You need to get the url response suggested file name and rename the file.
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
extension URL {
var typeIdentifier: String? { (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier }
}
let url = URL(string: "https://i.stack.imgur.com/varL9.jpg")!
URLSession.shared.downloadTask(with: url) { location, response, error in
guard let location = location,
let httpURLResponse = response as? HTTPURLResponse,
httpURLResponse.statusCode == 200 else { return }
let fileName = httpURLResponse.suggestedFilename ?? httpURLResponse.url?.lastPathComponent ?? url.lastPathComponent
let destination = FileManager.default.temporaryDirectory.appendingPathComponent(fileName)
do {
if FileManager.default.fileExists(atPath: destination.path) {
try FileManager.default.removeItem(at: destination)
}
print("location", location.typeIdentifier ?? "no UTI") // location dyn.ah62d4rv4ge81k5pu
try FileManager.default.moveItem(at: location, to: destination)
print("destination", destination.typeIdentifier ?? "no UTI") // destination public.jpeg
} catch {
print(error)
}
}.resume()
To complete the answer from #leo Dabus, there is a need to modify the destination URL further and append the file extension you desire by using
FileManager.default.moveItem(at:to:)
https://developer.apple.com/documentation/foundation/filemanager/1414750-moveitem
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
// create destination URL with the original pdf name
print("fileDownload: urlSession")
guard let url = downloadTask.originalRequest?.url else { return }
print("fileDownload: urlSession \(url)")
let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
// delete original copy
try? FileManager.default.removeItem(at: destinationURL)
// append a new extension type
let newURL = destinationURL.deletingPathExtension().appendingPathExtension("pdf")
// copy from temp to Document
try? FileManager.default.moveItem(at: destinationURL, to: newURL)
do {
try FileManager.default.copyItem(at: location, to: newURL)
myViewDocumentsmethod(PdfUrl:destinationURL)
print("fileDownload: downloadLocation", destinationURL)
} catch let error {
print("fileDownload: error \(error.localizedDescription)")
}
}
When I try to download the file from the url the files gets downloaded and is stored in files folder. But when I try to open the file. The file appears to be corrupted and I am not able to open the pdf file.
I have tried all the possibilites using the following code mentioned. But none of them worked. Any solution thanks in advance.
static func downloadFileFromUrl(urlString:String, fileName:String,viewController : UIViewController) {
// Create destination URL
if let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first{
let destinationFileUrl = documentsUrl.appendingPathComponent("\(fileName)")
//Create URL to the source file you want to download
let fileURL = URL(string: urlString)
let sessionConfig = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfig)
let request = URLRequest(url:fileURL!)
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
if let tempLocalUrl = tempLocalUrl, error == nil {
// Success
try? FileManager.default.removeItem(at: destinationFileUrl)
do {
try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
do {
//Show UIActivityViewController to save the downloaded file
let contents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
for indexx in 0..<contents.count {
if contents[indexx].lastPathComponent == destinationFileUrl.lastPathComponent {
let activityViewController = UIActivityViewController(activityItems: [contents[indexx]], applicationActivities: nil)
viewController.present(activityViewController, animated: true, completion: nil)
}
}
}
catch (let err) {
UIApplication.shared.keyWindow?.makeToast(message: err.localizedDescription)
}
} catch (let writeError) {
DispatchQueue.main.async(execute: {
UIApplication.shared.keyWindow?.makeToast(message: "Error creating a file :- \(writeError.localizedDescription)")
})
}
} else {
UIApplication.shared.keyWindow?.makeToast(message: "Error downloading the file")
}
}
task.resume()
}
}
Now the file after downloading is not opening. Is there any problem with code mentioned to download file from url
CompletionHandler of downloadTask Method is performed in Global (Background) Queue. You have to present your activityViewController in Main Queue.
Use the following code to present your activityViewController:
DispatchQueue.main.async(execute: {
let activityViewController = UIActivityViewController(activityItems: [contents[indexx]], applicationActivities: nil)
viewController.present(activityViewController, animated: true, completion: nil)
})
instead of just
let activityViewController = UIActivityViewController(activityItems: [contents[indexx]], applicationActivities: nil)
viewController.present(activityViewController, animated: true, completion: nil)
I'm trying to open a .pdf file after download which is downloaded with Alamofire. But I've seen only using a "webview". Thus the application consumes lots of memory and is not viable.
What I want is to open it with the native device application. Any suggestions? Thank you.
Edit: This is my code for download file:
var localPath: NSURL?
Alamofire.download(.GET, url, destination: { (temporaryURL, response) in
let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pathComponent = response.suggestedFilename
localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
return localPath!
})
.response { (request, response, _, error) in
if error != nil
{
// got an error in getting the data, need to handle it
print("Error: \(error!)")
}
//print(response)
print("Download file en:\(localPath!)")
self.view.hideToastActivity()
//self.actioncall()
}
}
I need open file from localpath...
You should use UIDocumentInteractionController. You can read about it on this Apple documentation page.
By doing some Googling you should see even some example implementations. For example here you can see some code about this done by "mattneub".
I let you one more code that you can use:
var documentInteractionController: UIDocumentInteractionController!
#IBAction func openDocument(sender: UIButton) {
let URL: NSURL = NSBundle.mainBundle().URLForResource("yourPDF", withExtension: "pdf")!
if (URL != "") {
// Initialize Document Interaction Controller
self.documentInteractionController = UIDocumentInteractionController(URL: URL)
// Configure Document Interaction Controller
self.documentInteractionController.delegate = self
// Present Open In Menu
self.documentInteractionController.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true)
//presentOpenInMenuFromRect
}
}
// UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self
}
I'm trying to open a .pdf file after download which is downloaded with Alamofire. But I've seen only using a "webview". Thus the application consumes lots of memory and is not viable.
What I want is to open it with the native device application. Any suggestions? Thank you.
Edit: This is my code for download file:
var localPath: NSURL?
Alamofire.download(.GET, url, destination: { (temporaryURL, response) in
let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let pathComponent = response.suggestedFilename
localPath = directoryURL.URLByAppendingPathComponent(pathComponent!)
return localPath!
})
.response { (request, response, _, error) in
if error != nil
{
// got an error in getting the data, need to handle it
print("Error: \(error!)")
}
//print(response)
print("Download file en:\(localPath!)")
self.view.hideToastActivity()
//self.actioncall()
}
}
I need open file from localpath...
You should use UIDocumentInteractionController. You can read about it on this Apple documentation page.
By doing some Googling you should see even some example implementations. For example here you can see some code about this done by "mattneub".
I let you one more code that you can use:
var documentInteractionController: UIDocumentInteractionController!
#IBAction func openDocument(sender: UIButton) {
let URL: NSURL = NSBundle.mainBundle().URLForResource("yourPDF", withExtension: "pdf")!
if (URL != "") {
// Initialize Document Interaction Controller
self.documentInteractionController = UIDocumentInteractionController(URL: URL)
// Configure Document Interaction Controller
self.documentInteractionController.delegate = self
// Present Open In Menu
self.documentInteractionController.presentOptionsMenuFromRect(sender.frame, inView: self.view, animated: true)
//presentOpenInMenuFromRect
}
}
// UIDocumentInteractionControllerDelegate
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self
}