Trying to open PDFs within my App

Screen Shot 2020-07-06 at 7.14.27 PM

I have started on a personal app with the primary focus of being able to navigate a number of menus in order to locate a specific & relevant pdf document. As you can see by the attached image, I presently have four screens; the first is a legal disclaimer which by clicking “I Agree” will take you to the main directory page (second screen). The third page/screen is a submenu page with the subtopics of the main topic picked from the main menu (page two). The fourth page takes you to the relevant buttons that I hope to link & open the pdf files based on the user’s selection from the third page. It is from the fourth page that I want to open a specific pdf file (by clicking the appropriate button).

Internet searches have found many methods (based on old versions of Swift & X Code) but do all have one thing in common - it appears that I will need to manually add code to the ViewController.swift file. However, when I highlight on the fourth page & open the assistant editor, I am not able to open the ViewController.swift file - I am only able to see a page of code entitled UIViewController.h (wish I could attach a second image but alas, as I a new user, I am not allowed).

The UIViewController.h page has tons of code (641 lines) & it appears that I am not able to add any code &/or link the push button to the code by pressing & holding control on the button within the view controller scene & drawing a line to the code area. Even if could, I would have no idea where to insert it within the 641 lines of code.

I am obviously doing something wrong but I have no idea what.

Help!

Please.

Hi Arnie,

The simplest way to get Xcode to open up in Assistant Editor mode AND make sure that it is linked to the right ViewController is to start with the storyboard selected.

Next is to hold the Option key down and then click on the ViewController.swift file you want to use to connect the outlets, actions, view etc to from the storyboard. This will work every time and cause you less grief than other ways of getting Xcode into Assistant Editor mode.

Well, it worked! However, when I tried to drag & link the button it would only allow me an “Exit function” not an action which, according to what I have read is what I need to do to open a pdf file.

Thanks for the tip though… no lesson is a bad lesson & I will use your tip in the future.

Here is an image of the UIViewController.h page that is associated with the fourth page/view in my app. I am now wondering if starting this project in a single view app was the right choice.

Problem solved but new hurdle now faced.

My initial problem was solved by creating a new file & copying the code from the ViewController.swift file & then associating the new ViewController file (uniquely named for the fourth view). This allowed me to create an action link for the pdf link button. HOWEVER, the code I found to display the PDF file throws a errors. The code is:

@IBAction func openPDF(_ sender: Any) {
let url = Bundle.main.url(forResource: “aa01Protocol”, withExtension: “pdf”)

if let url = url {
    let webView = WKWebView(frame: view.frame)
    let urlRequest = URLRequest(url: url)
    webView.load(urlRequest)
    view.addSubview(webView)
}

}

Any ideas?

If your intent is to only display a PDF file located at some URL(?) then rather than using a webView, I would consider importing SafariServices and using the API provided in that framework to display it.

I’m not at home at the moment so don’t have access to the code I have to make that work. I’m sure if you googled SafariServices you would find the code you need to do that.

Essentially what that API provides is a safari view with all the controls you need including a done button to return to the calling ViewController.

Here’s an article by Paul Hudson on using it.
https://www.hackingwithswift.com/read/32/3/how-to-use-sfsafariviewcontroller-to-browse-a-web-page

Thanks Chris!

I got it working but, I also noticed what you referred to… no back or done button. Thanks for the link! If you are willing, would be interested in seeing the code you referred to.

Regarding the Done button, to bring that into view just tap the top of the screen where the navigation bar would normally appear and the navigation bar will come into view.

Yeah, share your code by all means. Maybe post it to Dropbox and share a link.

Cheers.

I am assuming that to “tap” in the simulated iPhone it is a mouse click or two. If so, unfortunately a navigation bar did not come into view. Seems I am back on the hunt for a solution to this new problem.

I experimented with the Safari Service but could not get it to open my PDF. Guess that is just a bit above my skill level at this time. Hopefully I can figure out a way to insert a done or back button into the PDF view.

Thanks for all the help!

Not sure why it is not working for you but this is the code I use to launch the SafariViewController.

In your calling VC, import SafariServices

I have an extension to make calling the Safari View Controller easy:

extension UIViewController {
    
    func presentSafariVC(with url: URL) {
        let safariVC = SFSafariViewController(url: url)
        safariVC.preferredControlTintColor = .systemGreen
        present(safariVC, animated: true)
    }
}

Where I want to instantiate that VC I have a urlString which is just the text fo the url, ie:

let urlString = "https://www.imperialsupplies.com/pdf/i_drillsizedecimalequivalent&tapdrillchart.pdf"

The source of the urlString can be from wherever but the above is just an example for the exercise.
Next thing to do is to convert that to a URL, so:

let url = URL(string: urlString)

Then make the call to instantiate the SafariVC which calls that function in the extension shown above.

presentSafariVC(with: url!)

Using the url above with that PDF works perfectly.

Thanks!

I will give it a try & let you know how I do.

No luck with your code. I have attached an image of my code below.

Screen Shot 2020-07-07 at 11.11.27 PM

I just want to figure out how to add a close button so the PDF file closes & goes back to the fourth view screen (the screen that the PDF file was opened from). This learning process is interesting & fun but, also very frustrating. Quite a rush when a problem is solved but short lived as I am finding as a newbie learning Swift & Xcode… the problems are numerous.

I figured it out!

I embedded an navigation pane on the very first view of my app which then put a navigation bar (with a back button) on the following three views. Ok, can’t wait to see what my next hurdle will be.

1 Like