Ios – Path extension and MIME type of file in swift

iosmime-typesswift

I'm learning swift , and I made a table with cells that show an image ,and classes in groups by MIME TYPE (using path extension).
I had a question about extensions.
If the images (for example, it could be a video or a pdf) are taken from internet , i'm not sure if they have the extension or not
how can I get the MIMEtype of a file inside my file System without using path extension ?
PS :Sorry for my bad english it's not my native language

Best Answer

If anyone wants to get the MimeType from the actual URL of the file this code worked for me:

import MobileCoreServices

func mimeTypeForPath(path: String) -> String {
    let url = NSURL(fileURLWithPath: path)
    let pathExtension = url.pathExtension

    if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension! as NSString, nil)?.takeRetainedValue() {
        if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() {
            return mimetype as String
        }
    }
    return "application/octet-stream"
}