How to find file UTI for file, withouth pathExtension, in a path in Swift

You can use URL method resourceValues(forKeys:) to get the file URL TypeIdentifierKey which returns the file uniform type identifier (UTI). You just need to check if the returned value is “com.apple.m4v-video” for videos, “public.jpeg” or “public.png” for images and so on. You can add a typeIdentifier property extension to URL to return the TypeIdentifierKey string value as follow:

Xcode 11 • Swift 5.1

extension URL {
    var typeIdentifier: String? { (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier }
    var localizedName: String? { (try? resourceValues(forKeys: [.localizedNameKey]))?.localizedName }
}

Leave a Comment