What's the library should I import for UTTypeImage, which is the replacement of kUTTypeImage in iOS 15?

Viewed 2820

Before iOS 15, I used UIImagePickerController to capture images and video, and I got mediaType from [UIImagePickerController.InfoKey : Any], then I used kUTTypeImage (in the MobileCoreServices library) to identify the mediaType.

However, When it comes to iOS 15, Xcode complains that kUTTypeImage was deprecated in iOS 15.0. Use UTTypeImage instead. So, I replaced kUTTypeImage with UTTypeImage, but Xcode didn't know it.

Tried searching for some information, but didn't get any clue. I guess I should import the right library, but what is it?

Here is part of the code:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        guard let mediaType = info[.mediaType] as? String else { return }
        switch mediaType {
        case String(kUTTypeImage):
        // blabla
        case String(kUTTypeMovie):
        // blabla

and here are some screenshots: enter image description here

enter image description here

2 Answers

It's a bit confusing. First, you'll need to import UniformTypeIdentifiers. Then, replace kUTTypeImage with UTType.image (the Swift version of UTTypeImage).

If you are looking for a replacement of URL extension like in this post:

iOS15 UTType deprecations for URL-extension

here you go with a way shorter version


import Foundation
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"
    }
    
    
    func conforms(to type: UTType) -> Bool{
        let uttype = UTType(mimeType: mimeType())
        
        return uttype?.conforms(to: type) ?? false
        
        
    }
}

Usage:

url.conforms(to: .json)
Related