ShareLink is a button that presents the share sheet and exports its item declaratively. Any
type that conforms to Transferable, from the CoreTransferable framework, can be the item.
ShareLink
String, Data, and many other standard types are Transferable out of the box, so simple
exports need no extra code.
ShareLink(
"Export",
item: food.ingredients.joined(separator: ","),
preview: SharePreview("Export \(food.title)")
)
The preview parameter supplies the title the share sheet shows for the exported content.
Making a Type Transferable
A custom type conforms by implementing one requirement, the static transferRepresentation
property.
import CoreTransferable
extension Food: Transferable {
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(contentType: .text)
}
}
The framework ships four representations: CodableRepresentation exports a Codable type as
JSON, DataRepresentation fits a type you can convert to Data, FileRepresentation fits a
value that points at a file on disk, and ProxyRepresentation delegates to another
Transferable type.
Multiple Representations and Custom Types
transferRepresentation is a TransferRepresentationBuilder, so you can stack representations.
Order matters: put the most specific representation first, because receivers pick the first one
they understand.
import UniformTypeIdentifiers
extension UTType {
static var food: UTType {
UTType(exportedAs: "myapp.food.type")
}
}
extension Food: Transferable {
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(contentType: .food)
CodableRepresentation(contentType: .text)
}
}
The same conformance also powers drag and drop, so one Transferable implementation serves both
features.