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

2

25

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

Leopoldeen answered 26/9, 2021 at 6:33 Comment(0)
B
43

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

Butadiene answered 26/9, 2021 at 6:40 Comment(3)
And instead of case String(UTType.image):, I think it should be case UTType.image.identifier:.Andre
Im glad they are switching to using UniformTypeIdentifiers instead of the old MobileCoreServicesLatishalatitude
For Objective C, this answer was helpful: https://mcmap.net/q/538716/-what-header-to-include-to-use-uttype-in-objective-cChapbook
L
3

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)
Larynx answered 20/2, 2022 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.