Getting Local File’s Type Identifier From URL in iOS & macOS Apps

Updated on March 4th, 2021

⏱ Reading Time: 3 mins

When creating iOS and macOS apps, there are often times where it’s necessary to determine what kind a local file is; is it an image, video, audio, text, something else? Usually, the only available asset is the URL to the file, so how can we really get that information?

The truth is that it’s actually really easy to extract the type identifier of a local file just from the URL. Here is how:

Let’s explain what the above line of code does:

  • URL type has a collection of values about the resource it regards. In case of files, these values can provide the type identifier, creation or modification date, whether it’s a directory or not, and a lot more. You can find them all here.
  • The means to access these resource values is by calling the resourceValues(forKeys:) method on any URL object. Read more about this method here.
  • The argument that we must provide to this method is a set of URL resource keys; to fetch the type identifier, the provided set must contain the typeIdentifierKey value.
  • The return value of the resourceValues(forKeys:) method is a URLResourceValues object. To get the type identifier, all we need to do is to access the typeIdentifier property.

There are two important things to note however; the first is that the resourceValues(forKeys:) method might return nil in case the asked resource value does not exist. So, the type identifier is an optional value that must be unwrapped before it gets used.

The second is that resourceValues(forKeys:) can throw an exception, so it has to be called with the try keyword either as shown above, or in a do-catch statement.

Finding related types based on the extracted type identifier

Sometimes it’s useful to know the general type that the file type conforms to. For example, you might not care if a file is a PNG, JPEG or TIFF image, but all you care about is to know whether it’s actually an image.

In iOS 14 there’s a new structure to use when dealing with type identifiers, called UTType. It provides a handy property called supertypes, which returns a set of all types that the given type conforms to. For instance, an MP3 file with type identifier public.mp3, conforms to the following other types:

  • public.audio
  • public.audiovisual-content
  • public.data
  • public.content
  • public.item

The general type in this case is the public.audio. To get it, first it’s necessary to create a UTType object with the extracted type identifier from the URL, and by accessing the supertypes property to get the set of all related types. Since we are talking about a set, the contains() method can be used in order to determine if the file conforms to a specific type.

See the following sample code, where after fetching the type identifier of a URL, it gets the set with all related types and examines whether the type conforms to any major general type:

Note that it’s necessary to import the AVFoundation framework in order to make the UTType available in a source file.

Conclusion

In this post I demonstrated how to get the type identifier of a local file when the only thing you’ve got it’s the URL to it. In addition to that, I briefly mentioned the UTType and how to use it in order to get other types that a type conforms to. There’s definitely more to investigate on this topic if it interests you, as I just scratched the tip of the iceberg here.

Thanks for reading!

Stay Up To Date

Subscribe to my newsletter and get notifiied instantly when I post something new on SerialCoder.dev.

    We respect your privacy. Unsubscribe at any time.