ChallengerGuy ChallengerGuy - 1 year ago 137
Swift Question

Can't print PDF in webView using Swift 3, wants baseURL with initializers


project built in
had a feature where the user could print a PDF stored in app memory that was displayed in a
. Worked great and would print to the nearby wifi printer. I used the migration tool to convert it to
and it is now having problems with wanting a
that has initializers:

if buttonPressed == "Print" {

// Create a coded WebView only; not visible
let printWebView: UIWebView = UIWebView()

// Load the converted PDF Data to the WebView
printWebView.load(pdfData as Data, mimeType: "application/pdf", textEncodingName: "", baseURL: URL())



Worked fine before I started using
How do I fix this?, Previously it is working with
. The PDF is a saved on in the app memory that they can view and then print. Could the PDF itself act as a baseURL? I looked online and couldn't find anything that would help. Thank you for the help.

Answer Source

You can use absoluteURL property with empty NSURL() like this.

if let url = NSURL().absoluteURL {
    printWebView.load(pdfData as Data, mimeType: "application/pdf", textEncodingName: "", baseURL: url)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download