How to open and view url of documents file which is got from web service using QLPreviewController.
Asked
Active
Viewed 949 times
2 Answers
0
You can't do this. The documentation can explain well about this, but, you would like try to make your own viewer. Good luck!
See:
You can make as the site show: "Creating and Configuring a Document Interaction Controller"
- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL) fileURL
usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {
UIDocumentInteractionController *interactionController =
[UIDocumentInteractionController interactionControllerWithURL: fileURL];
interactionController.delegate = interactionDelegate;
return interactionController;}
"Once you have a document interaction controller, you can use its properties to get information about the associated file including its name, type, and URL."

Julio Amorim
- 43
- 1
- 12
-
Yes, but creating you own script. Without reources explicitly pf the language. – Julio Amorim Sep 27 '17 at 14:30
0
- Display some kind of progress spinner.
- Download the file to your
~/documents
folder usingURLSession
andURLSessionDownloadTask
. - Hide progress spinner.
- Then you need to create an instance of a class that implements
QLPreviewControllerDataSource
and holds your document. Hide progress spinner and present
QLPreviewController
import UIKit import QuickLook class Document: NSObject, QLPreviewItem { var previewItemURL: URL? } class PreviewDataSource: NSObject, QLPreviewControllerDataSource { var document: Document? func numberOfPreviewItems(in controller: QLPreviewController) -> Int { return document != nil ? 1 : 0 } func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { return document! } } class ProgressLoadingViewController: UIViewController { var session: URLSession = URLSession(configuration: URLSessionConfiguration.default) var downloadTask: URLSessionDownloadTask? var dataSource: PreviewDataSource? override func viewDidLoad() { super.viewDidLoad() startDownload() } func startDownload() { // start progres spinner downloadTask = session.downloadTask(with: URL(string: "http://che.org.il/wp-content/uploads/2016/12/pdf-sample.pdf")!) { [weak self] (downloadLocation, response, error) in guard // check if download went correctly error != nil, let filename = response?.suggestedFilename, let downloadLocation = downloadLocation else { print("Something went wrong: \(error!)") return } // copy file let fileManager = FileManager.default let targetPath = URL(fileURLWithPath: filename, relativeTo: fileManager.temporaryDirectory) do { try fileManager.copyItem(at: downloadLocation, to: targetPath) } catch let fileError { print("Copying failed: \(fileError)") return } // prepare preview let document = Document() document.previewItemURL = targetPath self?.dataSource? = PreviewDataSource() self?.dataSource?.document = document let qlViewController = QLPreviewController() qlViewController.dataSource = self?.dataSource // hide progress spinner self?.present(qlViewController, animated: true) { self?.downloadTask = nil qlViewController.reloadData() } } downloadTask?.resume() } }
Of course this long completion function should be split up into several and then moved to its own class DocumentDownloadManager
.

orkoden
- 18,946
- 4
- 59
- 50