I want to use ShareLink to share a URL that is generated from an API call from an async function.
I tried something like this, and it doesn't work:
ShareLink (item: Task {await getSharingURL()} )
{ Image(systemName: "square.and.arrow.up") }
In apple's documentation they say:
Sometimes the content that your app shares isn’t immediately available. You can use FileRepresentation or DataRepresentation when you need an asynchronous operation, like a network request, to retrieve and prepare the content.
https://developer.apple.com/documentation/SwiftUI/ShareLink
But I am not sure how to use it, and would appreciate any example.
Here's how I solved it.
I created a shareable model of whatever the object is I want to create a link for. Pass it whatever parameters you need to create the url.
You will need to define the
fetch()
function.Then I made it conform to
Transferable
, specifically usingDataRepresentation
so that it can be processed asynchronously.Then to deploy this, you just place the
ShareableObject
in aShareLink
...There might be a more succinct abstraction. If so, I'd love to get the feedback for that in comments or in a second answer.