Open file with quicklook(download from url)

1.6k views Asked by At

Now I want to open file(ex:.docx,.txt,.ppt,.png) with quicklook, I download two files from my own server,I want to open the first file,but I'm not sure if there is a successful download, and I don't know how to use Quick Look Framework,and it say

Type 'BdDeatilViewController' does not conform to protocol 'QLPreviewControllerDataSource'

,but I have added the protocol of QLPreviewControllerDataSource,can anyone help me with it? Here are my codes,thanks!

class BdDeatilViewController:UIViewController,QLPreviewControllerDataSource
{


    let quickLookController = QLPreviewController()

    var fileUrlArray:[String] = []
    var urlArray : [URL] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        addUrlTofile(fileArray: bulletinBoard.attachments!)
        stringToUrl(stringArray: fileUrlArray)

        quickLookController.dataSource = self
    }

    @IBAction func openFile(_ sender: Any) {

        downloadFile(url: urlArray, atIndex: 0)
        quickLookController.currentPreviewItemIndex = 0
        navigationController?.pushViewController(quickLookController, animated: true)

    }

    //add profix url to file
    func addUrlTofile(fileArray:Array<String>)  {
        for file in fileArray {
            var newURL:String = ""
            newURL = "http://163.18.22.78:81/api/Donwload/" + file
            fileUrlArray.append(newURL)
        }
    }

    //convert string array to URL array
    func stringToUrl(stringArray:Array<String>) {
        for i in stringArray {
            let url = URL(string: i)
            urlArray.append(url!)
        }
    }

    //download file from server
    func downloadFile(url:[URL],atIndex:Int) {

        let requestURL = url[atIndex]
        let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL)
        let session = URLSession.shared
        let task = session.dataTask(with: urlRequest as URLRequest) {
            (data, response, error) -> Void in
            if let httpResponse = response as? HTTPURLResponse {
                let statusCode = httpResponse.statusCode
                if (statusCode == 200) || ((statusCode == 204)) {
                   print("Everyone is fine, file downloaded successfully.")
                }
            }
        }
        task.resume()
    }

    func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int {
        return urlArray.count
    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return urlArray[index] as QLPreviewItem
    }

}
1

There are 1 answers

0
Thomas Deniau On

You're giving QuickLook the elements in urlArray which are http:// URLs. You need to give it file URLs as Quick Look does not download the items itself. Use NSURLSessionDownloadTask instead of NSURLSessionDataTask to download the file to your disk, not to a data object, and give Quick Look the downloaded file's URL.

Additionally, Devanshu Saini is right: you're using the wrong QLPreviewController delegate method signatures.

You want these:

func numberOfPreviewItems(in: QLPreviewController)
func previewController(QLPreviewController, previewItemAt: Int)