iOS15 UTType deprecations for URL-extension [duplicate]
Asked Answered
S

3

7

Using Swift5.5, iOS15.0.1,

As of iOS15, I realised that there are quite some deprecations going on in relation to my existing URL-extension.

I didn't find any good documentation on how to re-write my existing extension.

Here is my current implementation with approx. 16 depreciation warnings that I have no idea on how to circumvent using iOS15. Any idea on this is highly appreciated!

extension URL {
    func mimeType() -> String {
        let pathExtension = self.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"
    }
    
    var containsImage: Bool {
        let mimeType = self.mimeType()
        guard let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeRetainedValue() else {
            return false
        }
        return UTTypeConformsTo(uti, kUTTypeImage)
    }
    
    var containsAudio: Bool {
        let mimeType = self.mimeType()
        guard let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeRetainedValue() else {
            return false
        }
        return UTTypeConformsTo(uti, kUTTypeAudio)
    }
    
    var containsVideo: Bool {
        let mimeType = self.mimeType()
        guard  let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeRetainedValue() else {
            return false
        }
        return UTTypeConformsTo(uti, kUTTypeMovie)
    }
}
Symonds answered 12/10, 2021 at 12:35 Comment(0)
E
8

Swift 5.5

import UniformTypeIdentifiers

extension URL {
    
    var mimeType: String {
        return UTType(filenameExtension: self.pathExtension)?.preferredMIMEType ?? "application/octet-stream"
    }
    
    func contains(_ uttype: UTType) -> Bool {
        return UTType(mimeType: self.mimeType)?.conforms(to: uttype) ?? false
    }

}

//Example:
let fileUrl = URL(string: "../myFile.png")!
print(fileUrl.contains(.image))
print(fileUrl.contains(.video))
print(fileUrl.contains(.text))
Encomium answered 17/1, 2022 at 17:32 Comment(0)
S
5

The iOS15 compatible re-write of my initial URL-extension looks like this:

import UniformTypeIdentifiers

extension URL {
    func mimeType() -> String {
        let pathExtension = self.pathExtension
        if let type = UTType(filenameExtension: pathExtension) {
            if let mimetype = type.preferredMIMEType {
                return mimetype as String
            }
        }
        return "application/octet-stream"
    }
    
    var containsImage: Bool {
        let mimeType = self.mimeType()
        if let type = UTType(mimeType: mimeType) {
            return type.conforms(to: .image)
        }
        return false
    }
    
    var containsAudio: Bool {
        let mimeType = self.mimeType()
        if let type = UTType(mimeType: mimeType) {
            return type.conforms(to: .audio)
        }
        return false
    }
    
    var containsMovie: Bool {
        let mimeType = self.mimeType()
        if let type = UTType(mimeType: mimeType) {
            return type.conforms(to: .movie)   // ex. .mp4-movies
        }
        return false
    }
    
    var containsVideo: Bool {
        let mimeType = self.mimeType()
        if let type = UTType(mimeType: mimeType) {
            return type.conforms(to: .video)
        }
        return false
    }
}
Symonds answered 24/11, 2021 at 13:56 Comment(0)
B
2

For compatibility with iOS 15 and earlier version for get right UUType, because kUTType was deprecated, you can use this code

var UTTypeID : Array = [ "url", "data", "plainText"]

 // iOS 15 Deprecated kUTType, use UTType
func UTTypeCompat(strID : String) -> String
{
    //0 - URL, 1 - Data, 2 - PlainText
    let indexUTType:Int? = UTTypeID.firstIndex(of: strID)
    
    if #available(iOS 15.0, *)
    {
        switch indexUTType {
        case 0:
            return UTType.url.identifier
        case 1:
            return UTType.data.identifier
        case 2:
            return UTType.plainText.identifier
        default:
            NSLog("Unsupported UUtype: \(strID)")
            break
        }
    }
    else
    {
        switch indexUTType {
        case 0:
            return kUTTypeURL as String
        case 1:
            return kUTTypeData as String
        case 2:
            return kUTTypePlainText as String
        default:
            NSLog("Unsupported UUtype: \(strID)")
            break
        }
    }
    // throw Exception
    return "Err"
}

And example how get Identifier for any version of iOS

    NSLog(UTTypeCompat(strID: "url"))
    NSLog(UTTypeCompat(strID: "data"))
    NSLog(UTTypeCompat(strID: "plainText"))
Bryophyte answered 15/10, 2021 at 11:36 Comment(1)
thank you Anonymous, I appreciate your input. It looks like a very clean solution. I'll definitively consider it.Symonds

© 2022 - 2024 — McMap. All rights reserved.