Meet Transferable

RSS for tag

Discuss the WWDC22 Session Meet Transferable

Posts under wwdc2022-10062 tag

12 Posts

Post

Replies

Boosts

Views

Activity

dropDestination does not work inside List
I've discovered an issue with using iOS 16's Transferable drag-and-drop APIs for SwiftUI. The dropDestination modifier does not work when applied to a subview of a List. This code below will not work, unless you replace the List with a VStack or any other container (which, of course, removes all list-specific rendering). The draggable modifier will still work and the item will drag, but the dropDestination view won't react to it and neither closure will be called. struct MyView: View { var body: some View { List { Section { Text("drag this title") .font(.largeTitle) .draggable("a title") } Section { Color.pink .frame(width: 400, height: 400) .dropDestination(for: String.self) { receivedTitles, location in true } isTargeted: { print($0) } } } } } Has anyone encountered this bug and perhaps found a workaround?
9
0
4.1k
Feb ’26
Set filename to use for "Save to Files" with ShareLink?
Isn't there no way to set the default filename to use when we want to save a DataRepresentation to a file? If I export to JSON, the filename is "JSON.json" is used by iOS, even if I set the name to use in SharePreview. struct ContentView: View {     let car = Car(id: UUID(), name: "911", items:                     [Item(id: UUID(),date: .now, desc: "oil change"),                      Item(id: UUID(),date: .now, desc: "Battery")])     var body: some View {         VStack {             ShareLink(item: car, preview: SharePreview(car.name))         }         .padding()     } } extension UTType {     static var car: UTType = UTType(exportedAs: "com.acme.cararchive") } struct Car: Codable {     let id: UUID     let name: String     let items: [Item] } extension Car: Transferable {     static var transferRepresentation: some TransferRepresentation {         DataRepresentation(contentType: .json) { archive in             try JSONEncoder().encode(archive)         } importing: { data in             try JSONDecoder().decode(Car.self, from: data)         }     } } struct Item: Codable {     let id: UUID     let date: Date     let desc: String }
4
0
2.8k
Nov ’24
Drag and Drop using SwiftUI
Overview I am bit confused regarding drag and drop on SwiftUI I think there are 2 approaches but I am stuck with both approaches WWDC22 When using the new draggable, dropDestination, Transferable API, only single items are draggable. Multiple items in a list are not draggable. I have filed a feedback FB10128110 WWDC21 I have faced a couple of issues for drag and drop introduced in WWDC21 (onDrag, onDrop, itemIdentifier), the Feedback ids are FB9854301, FB9854569, FB9855245, FB9855532, FB9855567, FB9855575. It contains sample projects, would really appreciate if someone could have a look it. Note: All feedbacks include a sample project with detail steps and some even have screenshots and videos Questions: If my approach is wrong or if I am missing something? Unfortunately I didn't manage to get a SwiftUI lab session (got cancelled), so please help me with these issues.
4
1
3.8k
Jul ’24
How to set the filename of Transferable according to each instance?
It seems that the modifier func suggestedFileName(_ fileName: String) -> some TransferRepresentation can only set the filename for the entire Transferable type. What I want is to set the filename for each instance. Every instance of a Transferable type shouldn't have the same filename. static var transferRepresentation: some TransferRepresentation { FileRepresentation(exportedContentType: .myType) { myTransferable in         let url = try await myTransferable.exportedFileUrl()         return SentTransferredFile(url) /* maybe set the specific filename on SentTransferredFile? */     } .suggestedFileName("Unnamed") // maybe this should be a fallback filename }
1
0
1.1k
Sep ’23
ShareLink and DataRepresentation
In my app, I'd like to be able to share a .csv file via ShareLink and Transferable. I watched the "Meet Transferable" WWDC22 video and it should be possible as the presenter demonstrated that use case. However, when I try this on my end, I am able to share the content but somehow it is treated by iOS as plaintext and when sharing by email or messages, it will just add the text content to the body. If I try to share via AirDrop, it creates a random filename with the .txt extension even though I specify .commaSeparatedText. The only way this somewhat works is when saving to files. It will save as a .csv file but the filename is set to "comma-separated values". Here's some code: struct MyArchive {     enum ValidationError: Error {         case invalid     }     var filename: String { return "myarchive.csv"     }          init(csvData: Data) throws {         //...     }     func convertToCSV() throws -> Data {         let test = "Name,Email\nPete,pete@example.com"         if let data = test.data(using: .utf8) {             return data         }         else {             throw ValidationError.invalid         }     }  } extension MyArchive: Transferable {     static var transferRepresentation: some TransferRepresentation {         DataRepresentation(contentType: .commaSeparatedText) { archive in             try archive.convertToCSV()         } importing: { data in             try MyArchive(csvData: data)         }     } } And in my View: struct View: View { var body: some View { //... let csv = MyArchive() ShareLink( item: csv, preview: SharePreview(                         csv.filename,                         image: Image(systemName: "doc.plaintext")                     ) ) } } I'm at the point that I wonder if I'm doing something wrong or this just doesn't work in iOS 16 beta 1. Any hints? Thanks!
5
0
4.3k
May ’23
Drag file from app to Finder on macOS
Is it possible under macOS to drag a file from an app into the Finder?  For example, I want to drag a CSV file from the app to the Finder, but the following code does not work. static var transferRepresentation: some TransferRepresentation {         FileRepresentation(exportedContentType: .commaSeparatedText) {_ in             SentTransferredFile(Bundle.main.url(forResource: "Demo", withExtension: "csv")!)         } } And I changed the exportedContentType to ".fileUrl", ".url" and ".data" but the code still does not work.
5
1
2.4k
Mar ’23
Problems with draggable and dropDestination using DataRepresentation in Transferable
I can't get my drag and drop with DataRepresentation to work with Transferable. I'm trying to drag and drop instances of DataSettings which is an NSManagedObject that conforms to NSSecureCoding. Here's my UTType: extension UTType { static var encoderSettings = UTType(exportedAs: "com.simulator.EncoderSettings") } Here's my conformance to Transferable: extension DataSettings: Transferable { var data: Data? { try? NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: true) } public static var transferRepresentation: some TransferRepresentation { /*DataRepresentation(contentType: .commaSeparatedText) { setting in let data = setting.data print("DataRepresentation: \(data)") return data! } importing: { data in print("data: \(data)") return DataSettings() }*/ DataRepresentation(contentType: .encoderSettings) { setting in let data = setting.data print("DataRepresentation: \(data)") return data! } importing: { data in print("data: \(data)") return DataSettings() } // ProxyRepresentation(exporting: \.title) } } Here's a view where I'm testing my drop destination: struct DropTest: View { @State var isDropTargeted = false var body: some View { Color.pink .frame(width: 200, height: 200) .dropDestination(for: EncoderSettings.self) { setting, location in print("\(setting)") return true } isTargeted: { isDropTargeted = $0 print("Got it!!!") } } } Here's my Info plist: The ProxyRepresentation (String) works but I need the actual Data. The dragging starts (i.e.: I can drag the view that has the .draggable with DataSettings) but I can't drop it on my DropTest view. I can drop it on a view or app that accepts the ProxyRepresentation. What am I missing?
2
0
1.1k
Feb ’23
[ShareSheet] Only support loading options for CKShare and SWY types.
ShareLink(                 item: Image(systemName: "star"),                 subject: Text("Sharing an icon with you"),                 message: Text("This icon is made by the IconShop app"),                 preview: SharePreview("Icon", image: Image(systemName: "star")))               {                 HStack {                   Text("Share Image")                   Spacer()                   Image(systemName: "square.and.arrow.up")                 }               } At "Mac Catalyst" env, will get the log: 2023-01-21 11:16:39.469261+0800 IconShop[67164:10060637] [ShareSheet] Only support loading options for CKShare and SWY types. 2023-01-21 11:16:39.469577+0800 IconShop[67164:10060637] [ShareSheet] error fetching item for URL:file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png : (null) 2023-01-21 11:16:39.472151+0800 IconShop[67164:10060637] [ShareSheet] error fetching file provider domain for URL:file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png : (null) 2023-01-21 11:16:39.472951+0800 IconShop[67164:10060901] [ShareSheet] error loading metadata for documentURL:file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png error:Error Domain=NSFileProviderInternalErrorDomain Code=0 "No valid file provider found from URL file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png." UserInfo={NSLocalizedDescription=No valid file provider found from URL file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png.} 2023-01-21 11:16:41.884666+0800 IconShop[67164:10060637] [NSExtension] Extension request contains input items but the extension point does not specify a set of allowed payload classes. The extension point's NSExtensionContext subclass must implement `+_allowedItemPayloadClasses`. This must return the set of allowed NSExtensionItem payload classes. In future, this request will fail with an error. 2023-01-21 11:16:42.286195+0800 IconShop[67164:10060899] [ShareSheet] Connection invalidated So I can not find "save to file" , "Shart to PhotoLibary" button in the share menu.
3
2
2.9k
Jan ’23
How to drag multiple items in a list
I have a list containing multiple cells. How to drag the selected items? struct FolderDetail: View { let folder: Folder? @ObservedObject var dataStore: DataStore @State private var selectedCarIDs = Set<Int>() var body: some View { if let folder { List(dataStore.cars[folder.id] ?? [], selection: $selectedCarIDs) { car in Text(car.name) .tag(car.id) .draggable(car.name) } } else { Text("no folder selected") } } }
2
1
2.1k
Dec ’22
SWIFT TASK CONTINUATION MISUSE: loadTransferable(for:) leaked its continuation!
I try use dropDestination(payloadType: String.self) in iOS 16 instead of  .onDrop(of: [.plainText], ..., in iOS 15, but it doesn't work and I have SWIFT TASK CONTINUATION MISUSE: loadTransferable(for:) leaked its continuation! import SwiftUI struct ContentView: View {     @State var text: String = "🍌🍌"         var body: some View {             HStack {                 //  Text to drag                 Text(text)                     .font(.title)                     .draggable(text)                 /*    .onDrag { NSItemProvider(object: self.text as NSItemProviderWriting) }*/                 //  Area to drop                 RoundedRectangle(cornerRadius: 10)                     .frame(width: 150, height: 150)                    .dropDestination(payloadType: String.self) { (strings: [String], location) in                         self.text = "Dropped My Bananas 🍌🍌!"                         return true                     }                  /*   .onDrop(of: [.plainText], isTargeted: nil, perform: { _ in                         self.text = "Dropped My Bananas 🍌🍌!"                         return true                     })*/             }         } } What wrong with my code?
2
2
2.2k
Aug ’22
dropDestination does not work inside List
I've discovered an issue with using iOS 16's Transferable drag-and-drop APIs for SwiftUI. The dropDestination modifier does not work when applied to a subview of a List. This code below will not work, unless you replace the List with a VStack or any other container (which, of course, removes all list-specific rendering). The draggable modifier will still work and the item will drag, but the dropDestination view won't react to it and neither closure will be called. struct MyView: View { var body: some View { List { Section { Text("drag this title") .font(.largeTitle) .draggable("a title") } Section { Color.pink .frame(width: 400, height: 400) .dropDestination(for: String.self) { receivedTitles, location in true } isTargeted: { print($0) } } } } } Has anyone encountered this bug and perhaps found a workaround?
Replies
9
Boosts
0
Views
4.1k
Activity
Feb ’26
Set filename to use for "Save to Files" with ShareLink?
Isn't there no way to set the default filename to use when we want to save a DataRepresentation to a file? If I export to JSON, the filename is "JSON.json" is used by iOS, even if I set the name to use in SharePreview. struct ContentView: View {     let car = Car(id: UUID(), name: "911", items:                     [Item(id: UUID(),date: .now, desc: "oil change"),                      Item(id: UUID(),date: .now, desc: "Battery")])     var body: some View {         VStack {             ShareLink(item: car, preview: SharePreview(car.name))         }         .padding()     } } extension UTType {     static var car: UTType = UTType(exportedAs: "com.acme.cararchive") } struct Car: Codable {     let id: UUID     let name: String     let items: [Item] } extension Car: Transferable {     static var transferRepresentation: some TransferRepresentation {         DataRepresentation(contentType: .json) { archive in             try JSONEncoder().encode(archive)         } importing: { data in             try JSONDecoder().decode(Car.self, from: data)         }     } } struct Item: Codable {     let id: UUID     let date: Date     let desc: String }
Replies
4
Boosts
0
Views
2.8k
Activity
Nov ’24
Drag and Drop using SwiftUI
Overview I am bit confused regarding drag and drop on SwiftUI I think there are 2 approaches but I am stuck with both approaches WWDC22 When using the new draggable, dropDestination, Transferable API, only single items are draggable. Multiple items in a list are not draggable. I have filed a feedback FB10128110 WWDC21 I have faced a couple of issues for drag and drop introduced in WWDC21 (onDrag, onDrop, itemIdentifier), the Feedback ids are FB9854301, FB9854569, FB9855245, FB9855532, FB9855567, FB9855575. It contains sample projects, would really appreciate if someone could have a look it. Note: All feedbacks include a sample project with detail steps and some even have screenshots and videos Questions: If my approach is wrong or if I am missing something? Unfortunately I didn't manage to get a SwiftUI lab session (got cancelled), so please help me with these issues.
Replies
4
Boosts
1
Views
3.8k
Activity
Jul ’24
How to set the filename of Transferable according to each instance?
It seems that the modifier func suggestedFileName(_ fileName: String) -> some TransferRepresentation can only set the filename for the entire Transferable type. What I want is to set the filename for each instance. Every instance of a Transferable type shouldn't have the same filename. static var transferRepresentation: some TransferRepresentation { FileRepresentation(exportedContentType: .myType) { myTransferable in         let url = try await myTransferable.exportedFileUrl()         return SentTransferredFile(url) /* maybe set the specific filename on SentTransferredFile? */     } .suggestedFileName("Unnamed") // maybe this should be a fallback filename }
Replies
1
Boosts
0
Views
1.1k
Activity
Sep ’23
ShareLink and DataRepresentation
In my app, I'd like to be able to share a .csv file via ShareLink and Transferable. I watched the "Meet Transferable" WWDC22 video and it should be possible as the presenter demonstrated that use case. However, when I try this on my end, I am able to share the content but somehow it is treated by iOS as plaintext and when sharing by email or messages, it will just add the text content to the body. If I try to share via AirDrop, it creates a random filename with the .txt extension even though I specify .commaSeparatedText. The only way this somewhat works is when saving to files. It will save as a .csv file but the filename is set to "comma-separated values". Here's some code: struct MyArchive {     enum ValidationError: Error {         case invalid     }     var filename: String { return "myarchive.csv"     }          init(csvData: Data) throws {         //...     }     func convertToCSV() throws -> Data {         let test = "Name,Email\nPete,pete@example.com"         if let data = test.data(using: .utf8) {             return data         }         else {             throw ValidationError.invalid         }     }  } extension MyArchive: Transferable {     static var transferRepresentation: some TransferRepresentation {         DataRepresentation(contentType: .commaSeparatedText) { archive in             try archive.convertToCSV()         } importing: { data in             try MyArchive(csvData: data)         }     } } And in my View: struct View: View { var body: some View { //... let csv = MyArchive() ShareLink( item: csv, preview: SharePreview(                         csv.filename,                         image: Image(systemName: "doc.plaintext")                     ) ) } } I'm at the point that I wonder if I'm doing something wrong or this just doesn't work in iOS 16 beta 1. Any hints? Thanks!
Replies
5
Boosts
0
Views
4.3k
Activity
May ’23
Drag file from app to Finder on macOS
Is it possible under macOS to drag a file from an app into the Finder?  For example, I want to drag a CSV file from the app to the Finder, but the following code does not work. static var transferRepresentation: some TransferRepresentation {         FileRepresentation(exportedContentType: .commaSeparatedText) {_ in             SentTransferredFile(Bundle.main.url(forResource: "Demo", withExtension: "csv")!)         } } And I changed the exportedContentType to ".fileUrl", ".url" and ".data" but the code still does not work.
Replies
5
Boosts
1
Views
2.4k
Activity
Mar ’23
Problems with draggable and dropDestination using DataRepresentation in Transferable
I can't get my drag and drop with DataRepresentation to work with Transferable. I'm trying to drag and drop instances of DataSettings which is an NSManagedObject that conforms to NSSecureCoding. Here's my UTType: extension UTType { static var encoderSettings = UTType(exportedAs: "com.simulator.EncoderSettings") } Here's my conformance to Transferable: extension DataSettings: Transferable { var data: Data? { try? NSKeyedArchiver.archivedData(withRootObject: self, requiringSecureCoding: true) } public static var transferRepresentation: some TransferRepresentation { /*DataRepresentation(contentType: .commaSeparatedText) { setting in let data = setting.data print("DataRepresentation: \(data)") return data! } importing: { data in print("data: \(data)") return DataSettings() }*/ DataRepresentation(contentType: .encoderSettings) { setting in let data = setting.data print("DataRepresentation: \(data)") return data! } importing: { data in print("data: \(data)") return DataSettings() } // ProxyRepresentation(exporting: \.title) } } Here's a view where I'm testing my drop destination: struct DropTest: View { @State var isDropTargeted = false var body: some View { Color.pink .frame(width: 200, height: 200) .dropDestination(for: EncoderSettings.self) { setting, location in print("\(setting)") return true } isTargeted: { isDropTargeted = $0 print("Got it!!!") } } } Here's my Info plist: The ProxyRepresentation (String) works but I need the actual Data. The dragging starts (i.e.: I can drag the view that has the .draggable with DataSettings) but I can't drop it on my DropTest view. I can drop it on a view or app that accepts the ProxyRepresentation. What am I missing?
Replies
2
Boosts
0
Views
1.1k
Activity
Feb ’23
[ShareSheet] Only support loading options for CKShare and SWY types.
ShareLink(                 item: Image(systemName: "star"),                 subject: Text("Sharing an icon with you"),                 message: Text("This icon is made by the IconShop app"),                 preview: SharePreview("Icon", image: Image(systemName: "star")))               {                 HStack {                   Text("Share Image")                   Spacer()                   Image(systemName: "square.and.arrow.up")                 }               } At "Mac Catalyst" env, will get the log: 2023-01-21 11:16:39.469261+0800 IconShop[67164:10060637] [ShareSheet] Only support loading options for CKShare and SWY types. 2023-01-21 11:16:39.469577+0800 IconShop[67164:10060637] [ShareSheet] error fetching item for URL:file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png : (null) 2023-01-21 11:16:39.472151+0800 IconShop[67164:10060637] [ShareSheet] error fetching file provider domain for URL:file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png : (null) 2023-01-21 11:16:39.472951+0800 IconShop[67164:10060901] [ShareSheet] error loading metadata for documentURL:file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png error:Error Domain=NSFileProviderInternalErrorDomain Code=0 "No valid file provider found from URL file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png." UserInfo={NSLocalizedDescription=No valid file provider found from URL file:///var/folders/tr/rw5_yr292bd7zvwhr83skzvc0000gn/T/io.aben.IconShop/.com.apple.uikit.itemprovider.temporary.zkrp0q/PNG%20image.png.} 2023-01-21 11:16:41.884666+0800 IconShop[67164:10060637] [NSExtension] Extension request contains input items but the extension point does not specify a set of allowed payload classes. The extension point's NSExtensionContext subclass must implement `+_allowedItemPayloadClasses`. This must return the set of allowed NSExtensionItem payload classes. In future, this request will fail with an error. 2023-01-21 11:16:42.286195+0800 IconShop[67164:10060899] [ShareSheet] Connection invalidated So I can not find "save to file" , "Shart to PhotoLibary" button in the share menu.
Replies
3
Boosts
2
Views
2.9k
Activity
Jan ’23
How to drag multiple items in a list
I have a list containing multiple cells. How to drag the selected items? struct FolderDetail: View { let folder: Folder? @ObservedObject var dataStore: DataStore @State private var selectedCarIDs = Set<Int>() var body: some View { if let folder { List(dataStore.cars[folder.id] ?? [], selection: $selectedCarIDs) { car in Text(car.name) .tag(car.id) .draggable(car.name) } } else { Text("no folder selected") } } }
Replies
2
Boosts
1
Views
2.1k
Activity
Dec ’22
What configuration do we need to do at receiver apps to be able to receive Transferable data
How can we make sure the receiver app will correctly receive our custom models? Do we need to make some changes to the Info.plist in the receiver app to make sure it register's to be a receiver of a certain custom model.
Replies
0
Boosts
0
Views
760
Activity
Aug ’22
SWIFT TASK CONTINUATION MISUSE: loadTransferable(for:) leaked its continuation!
I try use dropDestination(payloadType: String.self) in iOS 16 instead of  .onDrop(of: [.plainText], ..., in iOS 15, but it doesn't work and I have SWIFT TASK CONTINUATION MISUSE: loadTransferable(for:) leaked its continuation! import SwiftUI struct ContentView: View {     @State var text: String = "🍌🍌"         var body: some View {             HStack {                 //  Text to drag                 Text(text)                     .font(.title)                     .draggable(text)                 /*    .onDrag { NSItemProvider(object: self.text as NSItemProviderWriting) }*/                 //  Area to drop                 RoundedRectangle(cornerRadius: 10)                     .frame(width: 150, height: 150)                    .dropDestination(payloadType: String.self) { (strings: [String], location) in                         self.text = "Dropped My Bananas 🍌🍌!"                         return true                     }                  /*   .onDrop(of: [.plainText], isTargeted: nil, perform: { _ in                         self.text = "Dropped My Bananas 🍌🍌!"                         return true                     })*/             }         } } What wrong with my code?
Replies
2
Boosts
2
Views
2.2k
Activity
Aug ’22
ShareLink and PDFDocument
Dear All, How do I share a PDFDocument using the new ShareLink of the SwiftUI? The following code does not compute: let pdf = PDFDocument(data: data) ShareLink(items: pdf.dataRepresentation()!) Thanks.
Replies
2
Boosts
2
Views
4.5k
Activity
Jul ’22