0

I open a file using the QLPreviewController to preview it from within the Xamarin.Forms iOS app. I turn off the "Open In" button to prevent the file from being exported to the application in QLPreviewController. However, after the latest iOS update on my test device (iPad), I saw that when the title in the Navigation bar is pressed, a menu opens and the "Open In" option is shown there.

How do I turn this off?

enter image description here

Pelin Konaray
  • 272
  • 1
  • 3
  • 15
  • How are you disabling the share button in the first place? – Thomas Deniau Jan 02 '23 at 10:24
  • Check [this question](https://stackoverflow.com/questions/64716489/how-to-prevent-sharing-files-from-on-my-iphone-folder). I mentioned how to do it in the answer. After writing the answer I did some changes after ios updates. But basically I created a dependency service and used a custom QLPreviewController to open the file. And I changed "item.RightBarButtonItem.Enabled" value to close share button. – Pelin Konaray Jan 02 '23 at 10:41
  • Ok. This is unsupported so I would avoid doing this. If the share button moves somewhere else in the UI, your code will break. (I don’t have a solution, sorry, but I would not muck with Apple’s UI in this way) – Thomas Deniau Jan 03 '23 at 15:39
  • Thanks for suggestion but it's our customer request. So that I need to do it. – Pelin Konaray Jan 04 '23 at 05:19
  • 1
    I saw that you posted the same ['How to prevent to open title menu of QLPreviewController - Xamarin.Forms iOS?' thread in Q&A](https://learn.microsoft.com/en-us/answers/questions/1148213/how-to-prevent-to-open-title-menu-of-qlpreviewcont.html), let's focus on that one. – Zack Jan 04 '23 at 07:18
  • Thank you @DongzhiWang-MSFT, I saw it and I faced some issue and asked from there. Thanks. – Pelin Konaray Jan 04 '23 at 08:04
  • You're welcome, you can keep following it. – Zack Jan 04 '23 at 09:21
  • If you feel the need to duplicate questions on the internet, please always declare all links in every copy, so that the time of readers is not wasted. – halfer Feb 22 '23 at 14:03

1 Answers1

0

The only solution that worked for me without too much hacking was to add the QLPreviewController's view as the single subview of another parent view controller.

By that you can add whatever navigation or toolbar you want. Keep in mind that hacking away / updating / adding stuff into existing native UI elements might break whenever iOS is updated.

It would look something like that, I'm using it to preview and share PDF files. The PDF file was downloaded before to fileURL.

final class PreviewController: UIViewController, QLPreviewControllerDataSource {

    /// The `QLPreviewController` hosting the PDF preview.
    private var previewController = QLPreviewController(nibName: nil, bundle: nil)

    /// The `URL` pointing to the device's filesystem.
    let fileURL: URL

    // MARK: - Init

    /// Creates an instance of `PreviewController` showing the PDF file at the given path.
    /// - Parameters:
    ///   - fileURL: The `URL` of the PDF file.
    ///   - title: The title to show on the view controller.
    init(fileURL: URL, title: String?) {
        self.fileURL = fileURL
        super.init()
        self.title = title
        setShareButton()
        previewController.dataSource = self
    }

    required init?(coder: NSCoder) { nil }

    // MARK: - Setup

    override func viewDidLoad() {
        super.viewDidLoad()
        addPreview()
    }

    /// Adds PDF preview to view hierarchy.
    private func addPreview() {
        addChild(previewController)
        view.addSubview(previewController.view)
        previewController.didMove(toParent: self)
        // +Setup constraints or set previewController.view.frame = view.frame here
    }

    /// Sets share button.
    private func setShareButton() {
        let shareButton = UIBarButtonItem(
            // It looks a little bit different to the original icon, apply scaling if needed.
            image: UIImage(systemName: "square.and.arrow.up"),
            primaryAction: .init(handler: { _ in
                self.tappedShareButton()
            }))
        navigationItem.leftBarButtonItem = shareButton
    }

    /// Share button handler method.
    private func tappedShareButton() {
        let shareSheet = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
        present(shareSheet, animated: true)
        shareSheet.popoverPresentationController?.barButtonItem = navigationItem.leftBarButtonItem
    }

    // MARK: - QLPreviewControllerDataSource

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        1
    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        fileURL as QLPreviewItem
    }
}

laka
  • 644
  • 8
  • 23