SwiftUI Link to open Bundle files in browser

You can get the browser to open a URL with one line of code...

Link("Info", destination: URL(string: "https://my.web.address/info.pdf")!)

You can compile and build this....

Link("Info", destination: Bundle.main.url(forResource: "info", withExtension: "pdf")!)

No errors or crashes, but it does nothing. The Bundle.main.url() reply makes sense within the application, but does not work in the browser, which is not in the application. This was a security feature to stop people pulling data from your application, according to an answer from about 10 years back.

I have written a WKWebView page within my application to display the file. This works, but it took a lot more than one line. I would have actually preferred my original solution, if there had been some way for me to mark certain files as suitable for public release. As well as the manual, I have a PDF test chart image, and a table of CSV data that I would like to make public like this. I could use a button in the app to open them in the browser, and the user could then save a copy when and where they wanted. The user gets to re-use their browser skills, and I write one line of code per file. Win-win, right?

Can we open a Bundled file in the browser using a Link using some trick I haven't found yet? Is there a good reason why we must not do this? Or what?

Done using WKWebView. A bit longer but it works. If you have links, you will have to support goBack() as it is not provided by default, and you get stuck in the link.

struct WebView: UIViewRepresentable {

    typealias UIViewType = WKWebView
    var webView: WKWebView
 
    init() {
        webView = WKWebView(frame: .zero)
        webView.load(URLRequest(url: Bundle.main.url(forResource: "ByEye", withExtension: "pdf")!))
    }

    func makeUIView(context: Context) -> WKWebView { webView }
    func updateUIView(_ uiView: WKWebView, context: Context) { }
}

struct ManualView: View {
    var webView = WebView()

    var body: some View {
        webView
    }
}
SwiftUI Link to open Bundle files in browser
 
 
Q