iOS15 URL扩展的UTType弃用



使用Swift5.5、iOS15.0.1、

从iOS15开始,我意识到有很多关于我现有URL扩展的贬低。

我没有找到任何关于如何重写现有扩展的好文档。

这是我目前的实现,大约有16个折旧警告,我不知道如何使用iOS15规避。任何关于这方面的想法都将不胜感激!

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

iOS15兼容的初始URL扩展重写如下:

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

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

为了与iOS 15和早期版本兼容,获得正确的UUType,因为kUTType已被弃用,您可以使用以下代码

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

以及示例如何获取任何版本的iOS 的标识符

NSLog(UTTypeCompat(strID: "url"))
NSLog(UTTypeCompat(strID: "data"))
NSLog(UTTypeCompat(strID: "plainText"))

相关内容

  • 没有找到相关文章

最新更新