In SwiftUI, how does dropDestination deliver dropped Transferable items, and what does the DropSession-based variant added in iOS 26 change?
answer
- typed values, not item providers
- a point and a Bool, then a session
- hover feedback for a highlight
- inside or outside your own app
- what an iOS 17 target still calls
basics
~20 sdropDestination(for: T.self) builds an array of T from the dropped content through T's import representations. The iOS 16 form also passes the drop location, returns a Bool and reports hover through isTargeted; iOS 26's dropDestination(for:isEnabled:action:) passes a DropSession instead.
solid answer
~40 s`dropDestination(for:action:isTargeted:)` (iOS 16) declares the payload type up front; SwiftUI imports the dropped content through that type's `Transferable` conformance and calls `action` with `[T]` and the drop's `CGPoint`, and you return whether you handled it. `isTargeted` reports a drag entering and leaving, so you can highlight the target. iOS 26 adds `dropDestination(for:isEnabled:action:)`: its action returns nothing and receives a `DropSession` with `location`, `itemsCount`, `phase` and `localSession`, which is `nil` for a drag from another app; iOS 27 adds `onDropSessionUpdated(_:)` to follow the phases. The SDK marks the old form deprecated in favor of the session one but without a release number, so the compiler stays silent. An app targeting iOS 17 keeps it and branches with `if #available(iOS 26.0, *)`. On a `ForEach`, `dropDestination(for:action:)` passes an insertion offset instead.
code
swift · 30 linesimport SwiftUI
struct MoodCard: Identifiable, Transferable {
var id = UUID()
var caption: String
static var transferRepresentation: some TransferRepresentation {
ProxyRepresentation { $0.caption } importing: { MoodCard(caption: $0) }
}
}
struct DropBoard: View {
@State private var cards: [MoodCard] = []
@State private var isTargeted = false
var body: some View {
let board = List(cards) { Text($0.caption) }
.border(isTargeted ? Color.accentColor : .clear)
if #available(iOS 26.0, *) {
board.dropDestination(for: MoodCard.self) { dropped, session in
print(session.itemsCount, session.localSession != nil)
cards.append(contentsOf: dropped)
}
} else {
board.dropDestination(for: MoodCard.self) { dropped, _ in
cards.append(contentsOf: dropped)
return !dropped.isEmpty
} isTargeted: { isTargeted = $0 }
}
}
}go deeper
Recall that dropDestination takes the payload type and gives your closure an array of that type plus where it landed.
Explain the action's Bool, the isTargeted closure, the ForEach variant with an insertion offset, and what DropSession adds in iOS 26.
Adopt the session API behind an availability check while iOS 17 devices keep the iOS 16 form, and explain why the compiler shows no deprecation warning yet.
Set the team's rule for deprecations that carry no release: track Apple's documentation, not only compiler warnings, and tie removal to the deployment-target bump.
## The drop side of a transfer **`dropDestination`** turns a SwiftUI view into a drop target for a `Transferable` type. Unlike the older `onDrop(of:isTargeted:perform:)`, which hands you `NSItemProvider` instances to load yourself, it names the payload type, and SwiftUI uses that type's import representations to build values for you. Apple's drag-and-drop documentation says to prefer transferable items in Swift. ## The iOS 16 form ```swift .dropDestination(for: MoodCard.self) { cards, location in board.append(contentsOf: cards) return true } isTargeted: { hovering in isHighlighted = hovering } ``` - **`for:`** names the payload type; values are created through its importing representations. - **`action`** receives the dropped items as `[T]` and the drop location as a `CGPoint`, and returns a `Bool` that reports whether you handled the drop. - **`isTargeted`** is called with `true` when a drag enters the view and `false` when it leaves; it defaults to a closure that does nothing. Views built with `ForEach` have their own variant, `dropDestination(for:action:)` on `DynamicViewContent`, whose action receives the items and an `Int` offset at which to insert them. The location-based form is marked unavailable there, with a message pointing to the offset variant. ## Compared with the item-provider API | Aspect | `onDrop(of:isTargeted:perform:)` | `dropDestination(for:action:isTargeted:)` | |---|---|---| | What you declare | An array of `UTType`s | One `Transferable` type | | What the action receives | `[NSItemProvider]` | `[T]`, already converted | | Hover feedback | an optional `Binding<Bool>` | a closure taking a `Bool` | | Loading | You start loading each provider inside the closure | SwiftUI converts through `T`'s conformance | The `onDrop` documentation adds a rule that shows why the typed API is easier: loading must start inside the closure, because for security reasons the receiver can access the dropped payload only before that closure returns. It also points to `dropDestination(for:isEnabled:action:)` for `Transferable` types and `DropSession` support. ## The iOS 26 form and DropSession iOS 26 adds `dropDestination(for:isEnabled:action:)`. Its action receives the items and a **`DropSession`**, and returns nothing: | Feature | iOS 16 form | iOS 26 form | |---|---|---| | Signature | `dropDestination(for:action:isTargeted:)` | `dropDestination(for:isEnabled:action:)` | | Second action parameter | `CGPoint` location | `DropSession` | | Action return value | `Bool` | none | | Hover feedback | `isTargeted` closure | the session's `phase`, followed with `onDropSessionUpdated(_:)` from iOS 27 | | Switching the target off | apply the modifier conditionally | `isEnabled:` parameter, default `true` | `DropSession` exposes: - `location` and `size` of the drop; - `itemsCount`, how many items the drag carries; - `phase`: `.entering`, `.active`, `.exiting`, `.ended(DropOperation)` or `.dataTransferCompleted`; - `localSession`, which is `nil` for a drag that came from elsewhere, so a non-nil value means it started inside your app; - `suggestedOperations`. ## Deprecation without a warning The SwiftUI interface marks the iOS 16 form deprecated with the message "Use `dropDestination(for:isEnabled:action:)` with an `action` that takes a `DropSession` parameter instead", and Apple's documentation lists it among deprecated view modifiers. The deprecation names no concrete release yet, so compiling it with the iOS 27 SDK, even for an iOS 27 deployment target, produces no warning. For an app that still supports iOS 17 or 18 it remains the drop API every device has: 1. Keep the iOS 16 form as the baseline. 2. Where you need session details, branch with `if #available(iOS 26.0, *)` and use the session form there. 3. Remove the old form when the deployment target reaches iOS 26, rather than waiting for a compiler warning. ## Things that go wrong - **Expecting an automatic highlight.** You draw the hover state yourself from `isTargeted`, or from the session's phase on newer systems. - **Item-provider habits.** `onDrop` hands you item providers, and its documentation notes the payload is only accessible before its closure returns; `dropDestination` gives you decoded values instead. - **Plain text that never becomes a model.** A drop of text produces your custom type only if that type has an importing representation for text, such as a `ProxyRepresentation` with an `importing:` closure. - **Putting the location form on a `ForEach`.** It is unavailable there; use the offset variant to insert at the drop position.
- How do you insert dropped cards at the position they land in a SwiftUI List?Attach `dropDestination(for: MoodCard.self) { dropped, offset in cards.insert(contentsOf: dropped, at: offset) }` to the `ForEach` inside the `List`. That `DynamicViewContent` variant, available since iOS 16, passes the insertion offset; the location-based form is marked unavailable on `ForEach`.
- How can a drop target tell a drag from its own app apart from one from another app?On iOS 26 and later, read `session.localSession` in the session-based action: it is `nil` for a drag that came from elsewhere and non-nil for one that started inside your app. The iOS 16 form has no equivalent, so an app that needs it branches with `if #available(iOS 26.0, *)`.
- dropDestination hands you NSItemProviders that you must load yourself.
- The iOS 16 dropDestination was removed in iOS 26, so old code no longer compiles.
- The isTargeted closure decides whether a drop is accepted.
- DropSession can be used on every iOS 17 device.
- The location-based dropDestination works the same when applied to a ForEach.
Assumes the iOS 27 SDK: the DropSession form and DropSession are iOS 26, onDropSessionUpdated is iOS 27, and the iOS 16 form is marked deprecated with no release set.