My network manager returns an URL. Inside this URL I have a pdf document and I want to show it in a quick look. But I am getting an error. Plus I could not figure out how to configure this quicklook in a proper way. I have tried integrating quicklook as below but I was unsuccessful.
class PersonalInfoDetailViewController: BaseViewController, QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
RtukNetworkManager.shared.getPayroll(year: selectedPayment.year, month: selectedPayment.month, salaryType: selectedPayment.paymentType) { [weak self] => // Return expression of type '()' does not conform to 'QLPreviewItem'
filePath in
DispatchQueue.main.async {
return filePath! as QLPreviewItem // => Cannot convert value of type 'QLPreviewItem' to closure result type 'Void'
}
}
}
for clarification this is my getPayroll
func getPayroll(year: Int, month: Int, salaryType: Int, completion: @escaping ((URL?) -> ())) {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
if let url = URL(string: "https://rip.rtuk.gov.tr/workplacebackend/api/Employee/GetPayroll?yil=\(year)&ay=\(month)&maasturu=\(salaryType)") {
let session = URLSession(configuration: .default)
let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)
if FileManager().fileExists(atPath: destinationUrl.path) {
print("File already exists [\(destinationUrl.path)]")
completion(destinationUrl)
}
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.addValue(authToken, forHTTPHeaderField: "Authorization")
request.addValue(workingToken, forHTTPHeaderField: "token")
let task = session.dataTask(with: request as URLRequest) { (data, response, error) in
if error == nil {
if let response = response as? HTTPURLResponse {
if response.statusCode == 200 {
if let data = data {
if let _ = try? data.write(to: destinationUrl, options: Data.WritingOptions.atomic) {
completion(destinationUrl)
}
else {
completion(destinationUrl)
}
}
}
}
} else {
print(error?.localizedDescription as Any)
completion(nil)
}
}
task.resume()
}
}
and finally that is my didSelectRowAt
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
switch cellType(rawValue: selected.title) {
case .salary:
selectedPayment = payment![indexPath.row]
let previewQL = QLPreviewController()
previewQL.dataSource = self
previewQL.currentPreviewItemIndex = indexPath.row
show(previewQL, sender: nil)
case .leave: break
case .personnel: break
case .none: break
}
previewItemAtneeds to return synchronously. Here you are returning your file from the block you dispatch_asynced, not from the delegate method. If you need to Quick Look a file that is not available yet, you should put up your own UI while it is downloading, before presenting the QLPreviewController. (I also remember that if you give a non-existent URL to Quick Look it will actually show a loading screen, and that you can call reloadData when the file is actually available, but I may be wrong).