ShareLink with AttributedString

The new ShareLink works great and saves a ton of code but I can't seem to get it to work with an attributedstring even though documentation indicates it should as attributedstring conforms to transferrable.

So for example:

//This works fine. ShareLink(item: "test")

//But this doesn't. ShareLink(item: AttributedString("test"))

Error instantly pops up "No exact matches in call to initializer".

Accepted Answer

Hi there! This compiler error is happening because there's no initializer for ShareLink that takes a Transferable item without the preview parameter.

When the shared item is a Transferable type, ShareLink needs to be given a SharePreview. (A preview isn’t required when sharing URLs or non-attributed strings. When sharing these types of content, the system can automatically determine a preview.)

You can use the preview to show your users a title or plain text version of the AttributedString being shared, along with optional image and/or icon.

ShareLink(
	item: AttributedString("Hello!"),
	preview: SharePreview("Hello!", image: Image(systemName: "doc.text")))
ShareLink with AttributedString
 
 
Q