Search code examples
iosswiftuiwebviewswift3base-url

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


My iOS project built in Swift2 using Xcode7 had a feature where the user could print a PDF stored in app memory that was displayed in a webView. Worked great and would print to the nearby wifi printer. I used the migration tool to convert it to Swift3 and it is now having problems with wanting a baseURL 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 Swift3 How do I fix this?, Previously it is working with NSURL(). 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.


Solution

  • 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)
    }