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.
Post not yet marked as solved
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.
Post not yet marked as solved
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!
Post not yet marked as solved
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
}
Post not yet marked as solved
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?
Post not yet marked as solved
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.
Post not yet marked as solved
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")
}
}
}