Hide right button n QLPreviewController?

后端 未结 8 1415
-上瘾入骨i
-上瘾入骨i 2021-01-17 01:35

I am subclassing QLPreviewController in my application and using the following code.

QLPreviewControllerSubClass* preview = [[QLPreviewControllerSubClass all         


        
相关标签:
8条回答
  • 2021-01-17 02:18

    Its simple and working well import quicklook and create a class of QL-Click here for Result which u all need, working well

    import UIKit
    import QuickLook.
    class QLSubclass: QLPreviewController , QLPreviewControllerDataSource {

    var p = NSURL()
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    override func viewDidLayoutSubviews() {
        navigationItem.rightBarButtonItems?[0] = UIBarButtonItem()
    }
    func show(controller: UIViewController, url: NSURL) {
        // Refreshing the view
        p = url
        self.dataSource = self
        self.reloadData()
        // Printing the doc
        if let navController = controller.navigationController {
            navController.pushViewController(self, animated: true)
        }
        else {
            controller.show(self, sender: nil)
        }
    }
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        let doc = p
        return doc
    }
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }
    

    }

    after then in your view controller class-

    class PatternVC: UIViewController, UIDocumentInteractionControllerDelegate , UINavigationControllerDelegate {

    var link = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        let url = "YOUR_URL_LINK"
      //check file exist in local or not------
        if  isFileExist(imgURL: url) {
         //Found then show
            let loadPath = loadDataFromDirectory(imgURL: url)
            showFileWithPath(path: loadPath)
        }else {
            //save in local
            saveDataOnDocumentDirectory(imgURL: url)
        }
    }
    override func viewDidLayoutSubviews() {
        self.navigationController?.setNavigationBarHidden(true, animated: false)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }//Open data
    func showFileWithPath(path: String){
        let isFileFound:Bool? = FileManager.default.fileExists(atPath: path)
        if isFileFound == true{
            QLSubclass().show(controller: self, url: URL(fileURLWithPath: path) as NSURL)
        }else{}}
    

    Also add a function for save in local in your view controller- //downloading request-----

    private var downloadTask: URLSessionDownloadTask?
    func saveDataOnDocumentDirectory(imgURL: String) {
        //let url = URL(string: imgURL)
        let escapedAddress = imgURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
        let url2 = URL(string: escapedAddress!)
        let sessionConfig = URLSessionConfiguration.default
        //let sessionConfig = URLSessionConfiguration.background(withIdentifier: "backgroundSession")
        let session: URLSession! = URLSession(configuration: sessionConfig, delegate: self, delegateQueue:  nil)
        downloadTask = session.downloadTask(with: url2!)
        downloadTask?.resume()
        DispatchQueue.main.async {
        }
    }}
    

    After that includes a extension on view extension PatternVC: URLSessionDelegate, URLSessionDownloadDelegate{

    func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
    
    }
    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        if error != nil {
            // lbl.text = "Download failed"
        }
        resetView()
    }
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        let fileManager = FileManager.default
        do {
            let requestURL = downloadTask.currentRequest?.url
            let documentDirectory = try fileManager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor:nil, create:true)
            let fileURL = documentDirectory.appendingPathComponent(getImageNameFromUrl(imgURL: requestURL!))
            do {
    
                try fileManager.moveItem(at: location, to: fileURL)
                print("save item: \(fileURL)")
            } catch (let writeError) {
                print("Error creating a file \(fileURL) : \(writeError)")
            }
        } catch {
            print(error)
        }
        DispatchQueue.main.async {
            //self.loadDirectory()
            self.resetView()
        }
    }
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        // 1
        //guard let _ = downloadTask.originalRequest?.url, let download = model?.imagePath  else { return }
        let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
        DispatchQueue.main.async {
            //self.radialView.ringProgress = CGFloat(progress * 100)
        }
        print("-URL: \(downloadTask.currentRequest?.url?.relativePath ?? "") ----Per: \(CGFloat(progress * 100))")
    }
    func resetView() {
        downloadTask!.cancel()
    }
    func isFileExist(imgURL: String) -> Bool{
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let url = NSURL(fileURLWithPath: path)
        let escapedAddress = imgURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
        let url2 = URL(string: escapedAddress!)
        if let pathComponent = url.appendingPathComponent(getImageNameFromUrl(imgURL: url2!)) {
            let filePath = pathComponent.path
            let fileManager = FileManager.default
            if fileManager.fileExists(atPath: filePath) {
                print("FILE AVAILABLE")
                return true
            } else {
                print("FILE NOT AVAILABLE")
                let url = APIConstant.videoJpgUrl + link
                //self.loadUrl (urlString:url)
                return false
            }
        } else {
            print("FILE PATH NOT AVAILABLE")
            return false
        }
    }
    func loadDataFromDirectory(imgURL: String) -> String
    {
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
        let url = NSURL(fileURLWithPath: path)
        let escapedAddress = imgURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
        let url2 = URL(string: escapedAddress!)
        if let pathComponent = url.appendingPathComponent(getImageNameFromUrl(imgURL: url2!)) {
            let filePath = pathComponent.path
            let fileManager = FileManager.default
            if fileManager.fileExists(atPath: filePath) {
                print("FILE AVAILABLE")
                return filePath
            } else {
                print("FILE NOT AVAILABLE")
                let url = APIConstant.videoJpgUrl + link
                self.loadUrl (urlString:url)
                return filePath
            }
        }else
        {
            return "ERROO"
        }
    }
    func getImageNameFromUrl(imgURL: URL) -> String{
        let name = imgURL.lastPathComponent
        let result = name.substring(from: name.index(name.startIndex, offsetBy: 5))
        return result
    }
    
    0 讨论(0)
  • 2021-01-17 02:19

    Thanks to Matthew Kostelecky, i was able to hide the share button but i would like to add some details for those need this in universal apps with multiple files.

    Firstly Matthew's answer works if you use QLPreviewController modally. If you push QLPreviewController from your navigation controller like this;

    navigationController?.pushViewController(quickLookController, animated: true)
    

    It wont be able to find any NavigationBar or ToolBar. You should call QLPreviewController modally like this;

    presentViewController(quickLookController, animated: true, completion: nil)
    

    Also if you are developing universal app and you have list of files to play. There will be another button(List Button);

    In iPhones if you have multiple files, QLPreviewController will create toolbar to show "List Button" and "Share Button" and both of these will be on ToolBar.
    In iPads both of these buttons are on NavigationBar and there is no ToolBar.

    So addition to Matthew's answer you should search toolBar if you have multiple files in iphone;

     func inspectSubviewForView(view: UIView) {
        for subview in view.subviews {
            if subview is UINavigationBar {
                // ** Found a Nav bar, check for navigation items. ** //
                let bar = subview as! UINavigationBar
    
                if bar.items?.count > 0 {
                    if let navItem = bar.items?[0] {
                        navItem.setRightBarButtonItem(nil, animated: false)
                    }
                }
            }
            if subview is UIToolbar {
                // ** Found a Tool bar, check for ToolBar items. ** //
                let bar = subview as! UIToolbar
                if bar.items?.count > 0 {
                    if let toolBarItem = bar.items?[0] {
                        toolBarItem.enabled = false
                    }
                }
            }
    
            if subview.subviews.count > 0 {
                // ** this subview has more subviews! Inspect them! ** //
                inspectSubviewForView(subview)
            }
        }
    }
    

    This piece of code will hide share button on iPad and disable share button on iPhone.

    Hope it helps to those still need it.

    0 讨论(0)
提交回复
热议问题