Skip to content

Sharing Content (SwiftUI)

A SwiftUI guide for coding agents. Also covers sharelink, share sheet swiftui, transferable protocol, export data ios, coretransferable, custom uttype export.

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.

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.

Use this guidance in your coding agent

Install the Better Design MCP once. Your agent then loads this page with one call.

get-swiftui-guide({ topic: "swiftui-sharing-content" })
claude mcp add --scope user better-design --transport http https://better-design.com/api/mcp --header "Authorization: Bearer <YOUR_API_KEY>"
Browse related design systems