How can you read a files MIME-type in objective-c

后端 未结 9 1629
伪装坚强ぢ
伪装坚强ぢ 2020-12-01 01:50

I am interested in detecting the MIME-type for a file in the documents directory of my iPhone application. A search through the docs did not provide any answers.

9条回答
  •  再見小時候
    2020-12-01 02:46

    Prcela solution did not work in Swift 2. The following simplified function will return the mime-type for a given file extension in Swift 2:

    import MobileCoreServices
    
    func mimeTypeFromFileExtension(fileExtension: String) -> String? {
        guard let uti: CFString = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension as NSString, nil)?.takeRetainedValue() else {
            return nil
        }
    
        guard let mimeType: CFString = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() else {
            return nil
        }
    
        return mimeType as String
    }
    

提交回复
热议问题