【问题标题】:Hide or disable share button from uidocumentinteractioncontroller in swift 5在swift 5中隐藏或禁用uidocumentinteractioncontroller的共享按钮
【发布时间】:2020-01-04 05:03:53
【问题描述】:

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

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

  1. hide share button from QLPreviewController
  2. UIDocumentInteractionController remove Actions Menu
  3. How to hide share button in QLPreviewController using swift?
  4. Hide right button n QLPreviewController?

请建议我实现这一目标。

这是我的演示代码:

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
    }
}


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

Here is the image

【问题讨论】:

    标签: ios swift swift5 uidocumentinteraction qlpreviewcontroller


    【解决方案1】:

    Find below 采用了我的方法来处理您的代码(进行了修改以在本地进行测试,但代码应该清晰)。这个想法是

    a) 重写,这是API完全允许的,需要类来拦截修改

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

    代码如下:

    // 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
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2018-01-18
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多