在 swift 5 中隐藏或禁用来自 uidocumentinteractioncontroller 的共享按钮 [英] Hide or disable share button from uidocumentinteractioncontroller in swift 5

查看:67
本文介绍了在 swift 5 中隐藏或禁用来自 uidocumentinteractioncontroller 的共享按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用 QuickLook 框架来查看文档文件,例如 pdf、ppt、doc 等.但由于隐私问题,我不希望用户可以与其他人所以请让我知道如何禁用/隐藏共享按钮以及复制粘贴选项.

In my application, I'm using the QuickLook framework to view the document files such as pdf, ppt, doc, etc. etc. But due to privacy concerns, I don't want that the user can share this document with others so please let me know how to disable/hide the share button and also the copy-paste option.

我知道这个问题可能会被问很多次并尝试了很多解决方案,但对我来说没有任何效果

I know this question can be asked by a number of times and tried many solutions but nothing works for me

  1. 隐藏 QLPreviewController 的分享按钮
  2. UIDocumentInteractionController 删除操作菜单
  3. 如何使用 Swift 在 QLPreviewController 中隐藏分享按钮?
  4. 隐藏右键 n QLPreviewController?

请建议我实现这一目标.

Please suggest to me to achieve this.

这是我的演示代码:

import UIKit
import QuickLook

class ViewController: UIViewController {
    
    lazy var previewItem = NSURL()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    @IBAction func displayLocalFile(_ sender: UIButton){
        
        let previewController = QLPreviewController()
        // Set the preview item to display
        self.previewItem = self.getPreviewItem(withName: "samplePDf.pdf")
        
        previewController.dataSource = self
        self.present(previewController, animated: true, completion: nil)
        
    }
    
    @IBAction func displayFileFromUrl(_ sender: UIButton){
        
        // Download file
        self.downloadfile(completion: {(success, fileLocationURL) in
            
            if success {
                // Set the preview item to display======
                self.previewItem = fileLocationURL! as NSURL
                // Display file
                let previewController = QLPreviewController()
                previewController.dataSource = self
                self.present(previewController, animated: true, completion: nil)
            }else{
                debugPrint("File can't be downloaded")
            }
        })
    }
    
    
    
    func getPreviewItem(withName name: String) -> NSURL{
        
        //  Code to diplay file from the app bundle
        let file = name.components(separatedBy: ".")
        let path = Bundle.main.path(forResource: file.first!, ofType: file.last!)
        let url = NSURL(fileURLWithPath: path!)
        
        return url
    }
    
    func downloadfile(completion: @escaping (_ success: Bool,_ fileLocation: URL?) -> Void){
        
        let itemUrl = URL(string: "https://images.apple.com/environment/pdf/Apple_Environmental_Responsibility_Report_2017.pdf")
        
        // then lets create your document folder url
        let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        
        // lets create your destination file url
        let destinationUrl = documentsDirectoryURL.appendingPathComponent("filename.pdf")
        
        // to check if it exists before downloading it
        if FileManager.default.fileExists(atPath: destinationUrl.path) {
            debugPrint("The file already exists at path")
            completion(true, destinationUrl)
            
            // if the file doesn't exist
        } else {
            
            // you can use NSURLSession.sharedSession to download the data asynchronously
            URLSession.shared.downloadTask(with: itemUrl!, completionHandler: { (location, response, error) -> Void in
                guard let tempLocation = location, error == nil else { return }
                do {
                    // after downloading your file you need to move it to your destination url
                    try FileManager.default.moveItem(at: tempLocation, to: destinationUrl)
                    print("File moved to documents folder")
                    completion(true, destinationUrl)
                } catch let error as NSError {
                    print(error.localizedDescription)
                    completion(false, nil)
                }
            }).resume()
        }
    }
    
}

//MARK:- QLPreviewController Datasource

extension ViewController: QLPreviewControllerDataSource {
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }
    
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        controller.navigationItem.rightBarButtonItem = nil
        return self.previewItem as QLPreviewItem
    }
}


请提供您的建议或任何其他框架来查看不同的文件格式.

Please provide your suggestion to do so or any other framework to view different file formats.

这是图片

推荐答案

发现下面采用了我的方法来处理你的代码(修改为本地测试,但代码应该清晰).这个想法是

Find below adopted my approach to your code (with modifications to test locally, but the code should be clear). The idea is

a) 覆盖,API 完全允许,需要的类来拦截修改

a) to override, which is completely allowed by API, needed classes to intercept modification

b) 有意使用自己的 UINavigationController,因为堆栈中只能有一个导航控制器

b) to use intentionally own UINavigationController, as only one navigation controller can be in stack

这里是代码:

// Custom navigation item that just blocks adding right items
class MyUINavigationItem: UINavigationItem {
    override func setRightBarButtonItems(_ items: [UIBarButtonItem]?, animated: Bool) {
        // forbidden to add anything to right
    }
}

// custom preview controller that provides own navigation item
class MyQLPreviewController: QLPreviewController {
    private let item = MyUINavigationItem(title: "")

    override var navigationItem: UINavigationItem {
        get { return item }
    }
}

class MyViewController : UIViewController, QLPreviewControllerDataSource {
    lazy var previewItem = NSURL()
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        // just stub testing code
        let button = UIButton(type: .roundedRect)
        button.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        button.setTitle("Show", for: .normal)
        button.addTarget(self, action: 
                         #selector(displayLocalFile(_:)), for: .touchDown)
        view.addSubview(button)
        self.view = view
    }

    @objc func displayLocalFile(_ sender: UIButton){

        let previewController = MyQLPreviewController() // << custom preview
        // now navigation item is fully customizable
        previewController.navigationItem.title = "samplePDF.pdf"
        previewController.navigationItem.leftBarButtonItem = 
            UIBarButtonItem(barButtonSystemItem: .done, target: self, 
                            action: #selector(closePreview(_:)))

        // wrap it into navigation controller
        let navigationController = UINavigationController(rootViewController: previewController)
        // Set the preview item to display
        self.previewItem = self.getPreviewItem(withName: "samplePDF.pdf")

        previewController.dataSource = self
        // present navigation controller with preview
        self.present(navigationController, animated: true, completion: nil)
    }

    @objc func closePreview(_ sender: Any?) {
        self.dismiss(animated: true) // << dismiss preview
    }

    func getPreviewItem(withName name: String) -> NSURL{

        //  Code to diplay file from the app bundle
        let file = name.components(separatedBy: ".")
        let path = Bundle(for: type(of: self)).path(forResource: file.first!, ofType: file.last!)
        let url = NSURL(fileURLWithPath: path!)

        return url
    }

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

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return self.previewItem as QLPreviewItem
    }

}

这篇关于在 swift 5 中隐藏或禁用来自 uidocumentinteractioncontroller 的共享按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆