I am having some issues trying to display a PDF which is stored in Firebase Storage in my SwiftUI app.
I have successfully done the following;
Uploaded a PDF file to Firebase Storage
Get the URL from the directory path
Create a PDFView and pass the URL
Currently the PDFKITVIEW appears but with nothing to show, and no errors. What am I missing ?
Show PDFKITVIEW
.sheet(isPresented: $isShowingDoc) {
PDFKitView(path: model.object.docURL) // Path to firebase storage is correct
}
PDFKITVIEW
import SwiftUI
import PDFKit
struct PDFKitView : UIViewRepresentable {
private var url: URL?
init(path: String) {
// Get url from path
let dm = DataManager()
url = dm.urlForDocPath(path: path)
}
func makeUIView(context: Context) -> UIView {
let pdfView = PDFView()
if let url = url {
pdfView.document = PDFDocument(url: url)
}
return pdfView
}
func updateUIView(_ uiView: UIView, context: Context) {
// Empty
}
}
DataManager
func getURLFromFirestore(path: String, success:#escaping (_ docURL:URL)->(),failure:#escaping (_ error:Error)->()){
let storage = Storage.storage().reference(withPath: path)
storage.downloadURL { (url, error) in
if let _error = error{
print(_error)
failure(_error)
} else {
if let docURL = url {
success(docURL)
}
}
}
}
func urlForDocPath(path: String) -> URL? {
var url: URL?
getURLFromFirestore(path: path, success: { (docURL) in
url = docURL
}) { (error) in
print(error)
}
return url
}
I cannot post this as a comment since I haven't got enough reputation to do so but seems like the problem can be in your urlForDocPath function, which tries to do an async operation but returning synchronously.
Could you check if url is not nil on the PDFKitView? Because if it is, it will prove my point.
Didn't tried yet in Xcode but needs to be something like following:
func urlForDocPath(path: String, success: #escaping (_ docURL:URL?)->(), failure: #escaping (_ error:Error)->()) {
var url: URL?
getURLFromFirestore(path: path, success: { (docURL) in
success(docURL)
}) { (error) in
failure(error)
}
}
Related
I'm very new in iOS development. So I'm trying to download pdf or image from firebase storage with URL using Alamofire and then I want to display with quicklook. I tried this example but no luck. Present preview of downloaded file directly in app
import UIKit
import Alamofire
import QuickLook
class DocumentViewer: UIViewController{
var previewItem = URL?.self
var refrenceDocURL: URL? // here i get the url from another view controller but never use it because I don't know where should I use it.
func downloadFile(fileUrl: URL) {
let destination: DownloadRequest.DownloadFileDestination = { _, _ in //ERROR: 'DownloadFileDestination' is not a member type of class 'Alamofire.DownloadRequest'
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent(documentFilename)
return (fileURL, [.removePreviousFile])
}
Alamofire.download(fileUrl, to: destination) //ERROR: Module 'Alamofire' has no member named 'download'
.response(completionHandler: { (downloadResponse) in
let previewController = QLPreviewController()
previewController.dataSource = self
self.previewItem = downloadResponse.destinationURL
self.present(previewController, animated: true, completion: nil)
})
}
}
extension DocumentViewer: QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return self.previewItem as! QLPreviewItem
}
}
Looks like you're using old definitions.
DownloadRequest.DownloadFileDestination is DownloadRequest.Destination.
Alamofire.download is AF.download.
this is my first time trying to add an AR preview into an app (also my first time using the file system). I have been trying to implement a solution similar to that explained here https://developer.apple.com/forums/thread/126377 however one key difference is that my usdz model is not in my main bundle as it is generated and downloaded from an external source at run time. I was wondering if it is possible to display a file stored in the apps documents or cache directory and how it is done.
The file is downloaded and stored in the caches directory as follows:
class ModelFetcher: NSObject{
var modelUrl: URL?
func generateModel() {
guard let url = URL(string: "http://127.0.0.1:5000/model.usdz") else {return}
let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())
var request = URLRequest(url: url)
request.httpMethod = "POST"
let downloadTask = urlSession.downloadTask(with: request)
downloadTask.resume()
}
}
extension ModelFetcher: URLSessionDownloadDelegate {
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
print("File Downloaded Location- ", location)
guard let url = downloadTask.originalRequest?.url else {
return
}
let docsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let destinationPath = docsPath.appendingPathComponent(url.lastPathComponent)
try? FileManager.default.removeItem(at: destinationPath)
do {
try FileManager.default.copyItem(at: location, to: destinationPath)
self.modelUrl = destinationPath
print("File moved to: \(modelUrl!.absoluteURL)")
} catch let error {
print("Copy Error: \(error.localizedDescription)")
}
}
}
I then try to display the model using ARQuicklookPreview as follows:
import SwiftUI
import QuickLook
import ARKit
struct ARQuickLookView: UIViewControllerRepresentable {
var allowScaling: Bool = true
func makeCoordinator() -> ARQuickLookView.Coordinator {
Coordinator(self)
}
func makeUIViewController(context: Context) -> QLPreviewController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
return controller
}
func updateUIViewController(_ controller: QLPreviewController,
context: Context) {
// nothing to do here
}
class Coordinator: NSObject, QLPreviewControllerDataSource {
let parent: ARQuickLookView
let destinationPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("model.usdz")
private lazy var fileURL: URL = destinationPath
init(_ parent: ARQuickLookView) {
self.parent = parent
super.init()
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(
_ controller: QLPreviewController,
previewItemAt index: Int
) -> QLPreviewItem {
let fileURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("model.usdz")
print(fileURL)
let item = ARQuickLookPreviewItem(fileAt: fileURL)
print(item)
item.allowsContentScaling = parent.allowScaling
return item
}
}
}
struct ARQuickLookView_Previews: PreviewProvider {
static var previews: some View {
ARQuickLookView()
}
}
However, I receive the error "Unhandled item type 13: contentType is: (null) #PreviewItem" in the console and in the UI it reads unsupported file type, I have performed tests to make sure the file is in the location of the URL and I can even open it in preview on my mac so it's not like the file format or location is wrong.
Any help on displaying a model from a location other than the main bundle would be helpful, or perhaps a workaround to first move the file then display it.
Edit: After changing the preview controller function to
let fileURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("model.usdz")
return fileUrl as QLPreviewItem
Now in the UI it just says "model universal scene description Package"
And in the console I still get an error however now it reads: Unhandled item type 13: contentType is: com.pixar.universal-scene-description-mobile #PreviewItem
Which doesn't make sense as that it one of the only types it supports
Thanks,
Louis
My application have file download option which downloads the file using alamofire download method. When the download completes i need to present the preview of the file without saving it to internal/cloud storage. How can i achieve this whatsapp like function that shows the preview after downloading the file.
func downloadFile(fileUrl: URL) {
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(fileUrl, to: destination)
.response(completionHandler: { (downloadResponse) in
self.dic.url = downloadResponse.destinationURL
self.dic.uti = downloadResponse.destinationURL!.uti
let rect = CGRect(x: 0, y: 0, width: 100, height: 100)
self.dic.presentOpenInMenu(from: rect, in: self.view, animated: true)
})
}
To present a preview of a file use Appleās QuickLook framework that lets you embed previewing for a huge range of file types, including iWork documents, Microsoft Office documents, PDFs, images, and more, all without writing much code.
First, import the QuickLook framework, then make your view controller conform to the QLPreviewControllerDataSource protocol.
Reference:
https://www.hackingwithswift.com/example-code/libraries/how-to-preview-files-using-quick-look-and-qlpreviewcontroller
https://github.com/gargsStack/QLPreviewDemo
https://www.appcoda.com/quick-look-framework/
Code:
class ViewController: UIViewController {
var previewItem = URL!
func downloadFile(fileUrl: URL) {
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)
Alamofire.download(fileUrl, to: destination)
.response(completionHandler: { (downloadResponse) in
let previewController = QLPreviewController()
previewController.dataSource = self
self.previewItem = downloadResponse.destinationURL
self.present(previewController, animated: true, completion: nil)
})
}
}
extension ViewController: QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return self.previewItem as QLPreviewItem
}
}
Here is one solution using Alamofire. Someone may help.
Steps:
Alamofire has excellent staff to direct download and also save/write
your file into disc.
Return a path where downloaded file saved.
Using UIDocumentInteractionController pass the file path
Then present this view
import Alamofire
extension UIViewController : UIDocumentInteractionControllerDelegate{
func downloadFileForPreview(fileName: String, fileExtension: String, filePath: String ) {
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileWithExtension = "file.\(fileExtension)"
let fileURL = documentsURL.appendingPathComponent(fileWithExtension)
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
//UtilitySwift.showUniversalLoadingView(true)
Alamofire.download(filePath, to: destination).response { response in
debugPrint(response)
//UtilitySwift.showUniversalLoadingView(false)
if response.error == nil, let storeFilePath = response.destinationURL?.path {
//let image = UIImage(contentsOfFile: imagePath)
self.previewDocument(withFilePath: response.destinationURL)
print(storeFilePath)
}
else{
UtilitySwift.showErrorMessage(message: response.error?.localizedDescription ?? "Error occured when downloading" )
print(response.error?.localizedDescription ?? "")
}
}
}
// Converted to Swift 5.1 by Swiftify v5.1.29672 - https://objectivec2swift.com/
func previewDocument(withFilePath filePath: URL?) {
var documentInteractionController: UIDocumentInteractionController?
if filePath != nil {
// Initialize Document Interaction Controller
if let filePath = filePath {
documentInteractionController = UIDocumentInteractionController(url: filePath)
}
// Configure Document Interaction Controller
documentInteractionController?.delegate = self as UIDocumentInteractionControllerDelegate
//if not possible to open
if !documentInteractionController!.presentPreview(animated: true) {
documentInteractionController?.presentOptionsMenu(from: CGRect.zero, in: self.view, animated: true)
}
} else {
// error
print("file preview error")
}
}
//UIDocumentInteractionControllerDelegate
public func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
self
}
}
Call from any UIViewController
self.downloadFileForPreview(fileName: "file", fileExtension: fileExt ?? "", filePath: REPLACE_WITH_DOWNLOAD_URL)
so this might be a trivial question, but I can't get it to work.
I want to save a pdf file to CoreData after I dropped it onto a view (using IOS' Drag&Drop feature)
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
session.loadObjects(ofClass: ComicBookPDFDocument.self) { (pdfItems) in
let items = pdfItems as! [ComicBookPDFDocument]
// "Cannot assign value of type 'ComicBookPDFDocument' to type 'NSData?'"
self.file.data = items[0]
}
}
ComicBookPDFDocument just subclasses PDFDocument to make it conforming to NSItemProviderReading:
final class ComicBookPDFDocument: PDFDocument, NSItemProviderReading {
public static var readableTypeIdentifiersForItemProvider: [String] {
return [kUTTypePDF as String]
}
public static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> ComicBookPDFDocument {
return ComicBookPDFDocument(data: data)!
}
}
However, I get this compiler error from XCode:
Cannot assign value of type 'ComicBookPDFDocument' to type 'NSData?'
How can I save the pdf data from a PDFDocument? I couldn't find anything on the internet or the documentation.
Thanks for any help
Okay, I don't know how I missed that, but here it is:
items[0].dataRepresentation()
You do one thing,
Try to save PDF into the Document Directory and save its path in the Core-Data.
Here is the code to save to Document directory and fetch from document direcory
class PDCache: NSObject {
static let sharedInstance = PDCache()
func saveData(obj: Data, fileName: String){
let filename = getDocumentsDirectory().appendingPathComponent("\(fileName).pdf")
do{
try obj.write(to: filename, options: .atomic)
} catch{
print(error.localizedDescription)
}
}
func getData(fileName: String) -> URL?{
let fileManager = FileManager.default
let filename = getDocumentsDirectory().appendingPathComponent("\(fileName).pdf")
if fileManager.fileExists(atPath: filename.path){
return URL(fileURLWithPath: filename.path)
}
return nil
}
private func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
}
To save the data use this
let data = Data()
self.saveData(obj: data, fileName: "myPdfFile")
and to get the file url use this
let pdfUrl = self.getData(fileName: "myPdfFile")
Try this and let me know if it works for you.
I have upload the image from my application and when I want to download its not able to cache.
My url : https://workmate.mapmyindia.com/app/mobile/displayImage/3b9c509d-6461-455d-8a2a-461adc73b81e
If I replace the string with other url, it works with following code.
var imageUrl : String = "https://workmate.mapmyindia.com/app/mobile/displayImage/3b9c509d-6461-455d-8a2a-461adc73b81e"
if let image = WMRequestManager().cachedImage(for: imageUrl) {
cell.taskImageView.image = image
}
else {
WMRequestManager().downloadImage(imageURL: imageUrl, success: { (img) in
cell.taskImageView.image = img
})
}
// MARK:- Image Download
func downloadImage(imageURL: String, success: #escaping (_ response:UIImage?) -> Void) {
let urlString = imageURL.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
Alamofire.request(urlString!, method: .get).responseImage { response in
guard let image = response.result.value else {
// Handle error
success(nil)
return
}
self.cache(image, for: urlString!)
success(image)
}
}
//MARK: - Image Caching
let imageCache = AutoPurgingImageCache(
memoryCapacity: UInt64(100).megabytes(),
preferredMemoryUsageAfterPurge: UInt64(60).megabytes()
)
func cache(_ image: Image, for url: String) {
imageCache.add(image, withIdentifier: url)
}
func cachedImage(for url: String) -> Image? {
return imageCache.image(withIdentifier: url)
}
extension UInt64 {
func megabytes() -> UInt64 {
return self * 1024 * 1024
}
}
I am stuck with, what the issue it will be. Is it a scaling issue or url issue. Is it a server side issue or client side.
If you can try use KingFisher (or something similar) that does the caching of the image for you. It will make your life a lot easier in the long run and will avoid your whole problem.