Instance Method

activityViewControllerLinkMetadata:

Returns metadata to display in the preview header of the share sheet.

Declaration

- (LPLinkMetadata *)activityViewControllerLinkMetadata:(UIActivityViewController *)activityViewController;

Parameters

activityViewController

The UIActivityViewController object requesting information about the item that the user wants to share.

Return Value

The LPLinkMetadata object that contains the metadata about a URL, including its title, icon, images, and video.

Discussion

Using the LinkPresentation framework, you can display rich previews of web links inside your app. For instance, if your app already has a database of links, with titles and images that weren’t fetched by LPMetadataProvider, you don’t have to fetch new metadata from the internet. Use this method instead and avoid downloading it again.

In your implementation, create an LPLinkMetadata object, and fill in at least the originalURL and URL fields, plus whatever additional information you have.

func activityViewControllerLinkMetadata(_: UIActivityViewController) -> LPLinkMetadata? {
    let metadata = LPLinkMetadata()
    metadata.originalURL = URL(string: "https://www.example.com/apple-pie")
    metadata.url = metadata.originalURL
    metadata.title = "The Greatest Apple Pie In The World"
    metadata.imageProvider = NSItemProvider.init(contentsOf:
        Bundle.main.url(forResource: "apple-pie", withExtension: "jpg"))
    return metadata
}

To learn more about presenting rich links and accelerating the share sheet, see LinkPresentation and LPMetadataProvider, or watch the WWDC 2019 session 262: Embedding and Sharing Visually Rich Links.