Unable to open a PDF document from with my app

My iOS app has a view where I list a number of links for further instructions. Linking to an external HTTP link is easy enough with a Link( destination:, label:). My challenge has been with local files which could be either an image or a PDF. I have this bit of code to deal with that:

struct FileLinkView: View {
    let link: InstructionLink
    @State private var contentToShow: AnyView?
    
    var body: some View {
        VStack {
            Button(action: {
                if (contentToShow == nil) {
                    openLocalFile(link.url)
                } else {
                    contentToShow = nil
                }
            }) {
                Text(link.label)
                    .foregroundColor(Color(.systemBlue))
            }
            .contentShape(Rectangle()) // Make the entire button tappable
            .padding(5)
            
            contentToShow?.padding()
        }
    }
    
    func openLocalFile(_ path: String) {
        let cleanedPath: String
        if path.hasPrefix("file://") {
            cleanedPath = path.replacingOccurrences(of: "file://", with: "")
        } else if path.hasPrefix("file:") {
            cleanedPath = path.replacingOccurrences(of: "file:", with: "")
        } else {
            cleanedPath = path
        }
        
        let fileURL = URL(fileURLWithPath: cleanedPath)
        guard UIApplication.shared.canOpenURL(fileURL) else {
            return
        }
        
        if fileURL.pathExtension.lowercased() == "pdf" {
            print("FileLinkView trying to open PDF ", fileURL.absoluteString)
            DispatchQueue.main.async {
                UIApplication.shared.open(fileURL)
            }
        } else {
            print("FileLinkView trying to open image ", cleanedPath)
            if let uiImage = UIImage(contentsOfFile: cleanedPath) {
                contentToShow = AnyView(Image(uiImage: uiImage).resizable().scaledToFit())
            }
        }
    }
}

The link url is in the form of "file://....." and points to files in subdirectories under the user's Documents directory. When opening the file with openLocalFile, I strip the url scheme off to use directly for images, and for a file URL for PDF.

(Images work great. They show up inline as the 'contentToShow', which can be toggled. PDF files should be shown externally (like an http link) and so I may have to clean up the way 'contentToShow' is used. Originally, I planned to have the PDF inline like the image but thought it might be better to us an external viewer (is it Safari?) to do the job. I'm still pretty new to Swift.)

Sadly, the PDF file does not show up even though the guard condition is passed and the resulting file URL printed in the console looks great. Here's a concrete example of that:

FileLinkView trying to open PDF  file:///Users/brian/Library/Developer/CoreSimulator/Devices/386A08A0-B581-4690-97E1-A5DCC532CDC3/data/Containers/Data/Application/B0491A25-69CE-47A1-B925-BB9E234383DF/Documents/checklog/blanks/My/Faves/instructions-brian/Express-Water-filter-manual.pdf

I set the project's info.plist parameters "supports document browser" and "supports opening documents in place" to YES. The File app on my simulator can see and open the pdf in my app's directory structure so I believe that the file is accessible enough.

And yet, when I click the button to show my PDF, nothing happens other than the printing in the console. Can someone please help me understand how to make this work? Thanks!

As an update, I tried to use the Link like a http url and am reserving my FileLinkView for images only (for now, as a test), hoping the Link view was clever enough to deal with both http and file url schemes. Here's that code...

struct InstructionLinksView: View {
    let links: [InstructionLink]

    var body: some View {
        ForEach(links, id: \.url) { link in
            if link.url.hasPrefix("http") {
                Link(destination: URL(string: link.url)!, label: {
                    Text(link.label)
                        .foregroundColor(Color(.systemBlue))
                })
            } else if link.url.hasPrefix("file") && link.url.hasSuffix("pdf") {
                Link(destination: URL(string: link.url)!, label: {
                    Text(link.label)
                        .foregroundColor(Color(.systemBlue))
                })
            } else if link.url.hasPrefix("file") {
                FileLinkView(link: link)
            }
        }
    }
}

No joy. I still get no PDF document. I'm missing something. Thanks for your help!

Hm, I used to get more help from this forum. Oh well, I managed to find a solution using a sheet and WKWebView. It's probably not what an expert would do but it should suffice.

Unable to open a PDF document from with my app
 
 
Q