Home > OS >  iOS15 UTType deprecations for URL-extension
iOS15 UTType deprecations for URL-extension

Time:10-16

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)
    }
}

CodePudding user response:

All the kUTType... constants and global C functions are replaced by the UTType struct. https://developer.apple.com/documentation/uniformtypeidentifiers/uttype

Just import UniformTypeIdentifiers and away you go.

Here's a simple example of converting a file extension to a mime type:

let ext = "txt"
if let type = UTType(filenameExtension: ext) {
// or: if let type = UTType(tag: ext, tagClass: .filenameExtension, conformingTo:nil) {
    if let mime = type.preferredMIMEType {
        print(mime) // "text/plain"
    }
}

CodePudding user response:

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"))
  • Related