iOS15 UTType deprecations for URL-extension [duplicate]
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)
}
}
Solution 1:
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
}
}
Solution 2:
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))