In SwiftUI, Uniform Type Identifiers (UTIs) are used to specify the type of data that can be passed between different parts of an app or between different apps.
UTIs are a way to describe the type of content or data that an app can accept or produce. They are strings in the format of "com.domain.filetype" and are typically associated with a specific file extension or MIME type.
For example, the UTI for a JPEG image file is "public.jpeg", while the UTI for a PDF file is "com.adobe.pdf". UTIs can also be used to describe abstract types of data, such as "public.image" for any type of image file, regardless of the specific file format.
In SwiftUI, UTIs are used in several places, such as when specifying the type of data that a Drag and Drop operation can handle, or when declaring the allowed file types for a DocumentPicker. By using UTIs, SwiftUI ensures that apps can communicate and exchange data in a consistent and interoperable way.
To use UTIs in SwiftUI, you can use the uniformTypeIdentifier(_:contentType:)
function on data types that conform to the PasteboardWriting
protocol to specify the supported UTIs. For example:
struct MyView: View, PasteboardWriter {
var utis: [String] {
["public.text", "public.image"]
}
var body: some View {
Text("Drag and drop here")
.onDrop(of: utis, isTargeted: nil) { providers in
// Handle dropped data
return true
}
}
}
In this example, MyView
supports both "public.text" and "public.image" UTIs and can handle dropped data of these types.
Overall, Uniform Type Identifiers are an important part of SwiftUI's data handling system, allowing apps to exchange data in a flexible and standardized way.