Core Transferable

RSS for tag

Declare a transfer representation for your model types to participate in system sharing and data transfer operations.

Core Transferable Documentation

Posts under Core Transferable tag

6 Posts
Sort by:
Post not yet marked as solved
0 Replies
80 Views
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.
Posted Last updated
.
Post not yet marked as solved
1 Replies
174 Views
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!
Posted
by Lucky7.
Last updated
.
Post not yet marked as solved
1 Replies
295 Views
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?
Posted
by WildGrey.
Last updated
.
Post not yet marked as solved
0 Replies
162 Views
I was wondering whether objects in Core Data (and CloudKit) conform, or are able to be conformed, to the Transferable protocol. As far as I can see there is no underlying implementation yet, but maybe this will be added as a future feature so that SwiftUI and Core Data can work better together. In the WWDC22 session "Enhance collaboration experiences with Messages" at 10:21, a struct call Note was implementing the transferRepresentation required property and returned an object of type CKShareTransferRepresentation from its body. I couldn't find any reference to this type anywhere in the documentation and nothing else was mentioned in the session video. Maybe this is something that is going to be added soon or was removed after the video was made. Since I want to use Core Data in my SwiftUI app and want to enable sharing features, such as collaboration via the shared database, NSManagedObject subclasses need some way of conforming to the Transferable protocol. I was hoping there would be a specialised way to do this, and the aforementioned type from the session video sort of hinted at this. I am just asking here to see if anyone has any information about this – whether this feature is about to be released, how this issue can be solved, or another way to do this. Any help is appreciated.
Posted
by BabyJ.
Last updated
.
Post not yet marked as solved
0 Replies
212 Views
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.
Posted
by new_bee.
Last updated
.
Post not yet marked as solved
0 Replies
111 Views
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") } } }
Posted
by new_bee.
Last updated
.