Ariel Gemilang Ariel Gemilang - 3 months ago 13
Swift Question

cannot view pdf to web view using file path

i have an app to download pdf file. If the download completed it have a path like

file:///Users/developer/Library/Developer/CoreSimulator/Devices/F9AB746D-20C3-4333-AE70-2840538F4AED/data/Containers/Data/Application/861FA32B-06AA-4176-A499-B4BB8DF224C1/Library/Caches/pdffile.pdf


I try to view using this

@IBOutlet weak var webView: UIWebView!

var path = ""
var receiveData: String = "" //this contain file:///Users/developer.../Caches/pdffile.pdf

override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL.fileURLWithPath(receiveData)
print(url)
self.webView.loadRequest(NSURLRequest(URL: url))
}


the build succeded and no error butstill can't view the pdf file

what's wrong?

Answer

Change your NSURL path, you need to use receiveData, because that already contain the path of the file, NSBundle is use when you have file inside your project directory.

 let url = NSURL.fileURLWithPath(receiveData)
Comments