在 swift 5 中隐藏或禁用 uidocument交互控制器中的共享按钮



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

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

  1. 从 QLPreviewController 隐藏共享按钮
  2. UIDocumentInteractionController remove Actions Menu
  3. 如何使用 swift 在 QLPreviewController 中隐藏共享按钮?
  4. 隐藏右键 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
}
}

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

这是图片

在下面找到采用了我对你的代码的方法(修改在本地测试,但代码应该很清楚(。这个想法是

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

最新更新