How could one produce an icon from a document (based on file type), selected via UIDocumentationPickerController?

484 Views Asked by At

I am new fairly new to swift development but am obsessed and in love with learning, I have not only dedicated myself to learning but I am starting to apply my knowledge and have built a small messaging app with the capability to select a file. I have successfully created the ability for future users of my app to send documents via the messaging system, and I am able to show the file name, type etc. However, I am struggling to understand (reading apple documentation and other community developer's posts regarding this) how to fetch a specific icon type from the iOS system based on the file's extension type (e.g. xlsx), let alone the UTIType (e.g. public.movie -> mp4, etc.).

If anyone has suggestions on how/where to facilitate this, I would like to set my current, generic file icon to a custom filetype, as the one in the attached image example below (PDF in example):

enter image description here

Tarun's answer was quick and very helpful in getting one step further, however, it returned this icon for pdf, png, and jpeg, all tested (regardless of the aforementioned differentiation in file type.

enter image description here

Here is my code:

guard let fileUrl = message.documentUrl else { return }
let fileNameUrl = (fileUrl as NSString).lastPathComponent
let fileName = fileNameUrl.components(separatedBy: "?")[0].removingPercentEncoding
    
let documentInteractionController = UIDocumentInteractionController()
documentInteractionController.name = fileName
documentInteractionController.url = URL(fileURLWithPath: fileNameUrl)
    
let fileTypeIcon = documentInteractionController.icons.first
fileIcon.image = fileTypeIcon
2

There are 2 best solutions below

9
Tarun Tyagi On

When you get a file url from UIDocumentPickerViewController, you need to query it's uti / mimeType etc. which you can do from following helpers.

import Foundation
import MobileCoreServices

public extension URL {
    
    private var utiCFString: CFString? {
        UTTypeCreatePreferredIdentifierForTag(
            kUTTagClassFilenameExtension, 
            self.pathExtension as NSString,
            nil
        )?.takeRetainedValue()
    }
    
    var uti: String? {
        self.utiCFString as String?
    }
    
    var mimeType: String? {
        if let utiCFString = self.utiCFString {
            return UTTypeCopyPreferredTagWithClass(
                utiCFString, 
                kUTTagClassMIMEType
            )?.takeRetainedValue() as String?
        }
        return nil
    }
    
}


extension ViewController: UIDocumentPickerDelegate {
    
     func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        
        guard let url = urls.first else { return }
            
        guard let mimeType = url.mimeType else {
            print("Invalid file type")
            return
        }

        let uti = url.uti
    }
}

Once you have all the info you need, you can try this -

let documentInteractionController = UIDocumentInteractionController()
documentInteractionController.name = filename
documentInteractionController.url = URL(fileURLWithPath: filePath)
documentInteractionController.uti = uti

let mayBeIcon = documentInteractionController.icons.last
0
Akilan C B On

You can use QuickLook Thumbnailing framework.Have a look at the Apple documentation. https://developer.apple.com/documentation/quicklookthumbnailing