I am creating and saving PDFs generated from UIImage to the device. I am also saving the path of that file in the Core Data. I can retrieve the path and check whether the file is present or not, however when I try to load the PDF in UIWebView, the screen returns blank. I have tried loading the request as well as loading data, but none of the approached has worked. I am applying this code to TableView didSelectRow function, so when user clicks on the cell, a reusable popup appears which will load the PDF. Below is my code.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedDoc = tableView.cellForRow(at: indexPath) as! DisplayMainSavedDataCell
let document : String = selectedDoc.documentNameLabel.text!
print(document)
let path = fetchDocumentURL(documentName: document)
let urlDoc = URL(fileURLWithPath: path)
print(urlDoc)
let request = URLRequest.init(url: urlDoc)
let sb = UIStoryboard.init(name: "Main", bundle: nil)
let popup = sb.instantiateViewController(withIdentifier: "pdfPopUp") as! PDFViewController
popup.webView?.loadRequest(request)
present(popup, animated: true)
}
func fetchDocumentURL (documentName: String) -> String {
let docName = documentName
let fetchRequest : NSFetchRequest<Document> = Document.fetchRequest()
let predicate = NSPredicate(format: "docName == %@", docName as CVarArg)
fetchRequest.predicate = predicate
do {
let results = try PersistenceService.context.fetch(fetchRequest)
for result in results as [Document] {
let path = result.docPath
return path!
}
} catch{
print("files does not exists or file missing")
let path = "No path exists"
return path
}
let path = "No path exists"
return path
}