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
}