I'm setting up an application that centrally receives the URL and name of a document/file (https://something.com/model.obj f.e) and "Model Name". Once it is being received, I download it using Alamofire and then push it to the desired ViewController. However, once it is downloaded, it won't display the file.
I've tried moving the downloading logic outside of the view, I've tried adding the view to as a child, but the problem persists.
Origin View Controller
func prepareQuickLookSegue(_ url: URL,_ name: String,_ viewController: UIViewController) {
SVProgressHUD.show()
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
documentsURL.appendPathComponent(url.lastPathComponent)
return (documentsURL, [.removePreviousFile])
}
Alamofire.download(url, to: destination).responseData { response in
if let destinationUrl = response.destinationURL {
SVProgressHUD.dismiss()
let vc = QuickLookViewController(url: destinationUrl, name: name)
viewController.navigationController?.pushViewController(vc, animated: true)
}
}
}
Quick Look View Controller
import UIKit
import QuickLook
class QuickLookViewController: UIViewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate {
let quickLookController = QLPreviewController()
var name:String?
var fileURL:NSURL?
init(url:URL,name:String){
self.fileURL = url as NSURL
self.name = name
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
quickLookController.dataSource = self
quickLookController.delegate = self
navigationItem.title = self.name
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return fileURL!
}
func previewController(controller: QLPreviewController, shouldOpenURL url: NSURL, forPreviewItem item: QLPreviewItem) -> Bool {
if item as? NSURL == fileURL {
return true
} else {
print("Will not open URL \(String(describing: url.absoluteString))")
}
return false
}
}
I expect an image / file to be shown, but a black image is shown.
The problem is that your QuickLookViewController never actually presents the quick look. You have a QLPreviewController but it just sits there, unused.
Since QLPreviewController is itself a view controller, I would suggest eliminating the QuickLookViewController and just show the QLPreviewController from the first view controller.