1

How to open and view url of documents file which is got from web service using QLPreviewController.

monali
  • 11
  • 1

2 Answers2

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:

Oficial topic IOS

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
0
  1. Display some kind of progress spinner.
  2. Download the file to your ~/documents folder using URLSession and URLSessionDownloadTask.
  3. Hide progress spinner.
  4. Then you need to create an instance of a class that implements QLPreviewControllerDataSource and holds your document.
  5. 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