Search code examples
iosswiftipadwkwebview

Why do my WKWebviews work on the simulator, but are unable to pull up the files on the iPad?


I am using a WKWebview to show a local file, and it works perfectly on the device simulator. However, when I start the app they files are available- I check it and have it print out the path if the file exists, however the webviews are unable to pull it up. Later on I use the data from that file to send it in an email and that function works great.

Here is the code relating to opening up the local file on the WKWebView:

 func loadPDF(){
    let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
    let url = NSURL(fileURLWithPath: path)
    if let pathComponent = url.appendingPathComponent("Survey-\(pdf.fileTitle!).pdf") {
        let filePath = pathComponent.path
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: filePath) {
            print("FILE AVAILABLE")
            print(filePath)
            let newURL = URL(fileURLWithPath: filePath)
            let request = URLRequest(url: newURL)
            pdfView.load(request)
        } else {
            print("FILE NOT AVAILABLE")
            print(filePath)
        }
    } else {
        print("FILE PATH NOT AVAILABLE")
    }

}

Solution

  • Since no one was able to answer this and I was quite stumped myself, I found that using the UIDocumentPicker was a way easier and more clean way of viewing my pdf files. I also was able to use the native share button instead of the one I hard coded myself.